jQuery AJAX POST request fails to execute in webhook when triggered by Zapier

I need help with a webhook issue. When Zapier sends data to my webhook, the jQuery AJAX POST requests inside the webhook don’t work properly. The target server doesn’t have PHP curl support, so I’m stuck.

Is there a way to make AJAX calls work from within a webhook without using PHP curl? Here’s what I’m working with:

<?php
$webhookData = json_decode(file_get_contents('php://input'), true);
$receivedData = json_decode($_POST['payload'], true);

$userEmail = '[email protected]';
$userName = 'John Doe';
$userPhone = '555-0123';
$dateCreated = '12/15/23 10:30AM';
$selectedBranch = 'Downtown';
$campaignName = 'Winter2023 - Eye Care';
$serviceCode = 'SVC12345';
$region = 'North America';
$leadSource = 'Google Ads';
$keyword = $receivedData['search_term'];

if($userEmail != '') {
?>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
var apiEndpoint = "https://api.example.com/v2/";

var branchLookup = {
    "apiKey": "ABC123DEF456",
    "apiVersion": "2.1",
    "searchType": "Branch Location"
};

fetchBranchData("locations/search", branchLookup, function(success, response) {
    if (!success) {
        console.log("API Error: " + response);
        return;
    }
    
    var targetBranch = "<?php echo $selectedBranch; ?>";
    
    $.each(response.results, function(index) {
        var branchName = response.results[index].title;
        
        if(branchName === targetBranch) {
            var branchId = response.results[index].id;
            
            var leadData = {
                "apiKey": "ABC123DEF456",
                "apiVersion": "2.1",
                "fullName": "<?php echo $userName; ?>",
                "phoneNumber": "<?php echo $userPhone; ?>",
                "emailAddress": "<?php echo $userEmail; ?>",
                "branchCode": branchId,
                "sourceType": "<?php echo $leadSource; ?>",
                "campaignId": "CAMP789",
                "serviceType": "<?php echo $serviceCode; ?>"
            };
            
            submitLead(leadData);
        }
    });
});

function fetchBranchData(endpoint, params, callback) {
    $.post(apiEndpoint + endpoint, params, function(data) {
        return callback(true, data);
    });
}

function submitLead(leadInfo) {
    sendToAPI("leads/submit", leadInfo, function(result, data) {
        // Handle response
    });
}

function sendToAPI(endpoint, params, callback) {
    $.post(apiEndpoint + endpoint, params, function(response) {
        // Process API response
    });
}
</script>
<?php
}
?>

What alternatives exist when Zapier triggers my webhook but the jQuery POST calls don’t execute? Any suggestions for different approaches would be appreciated.

The fundamental issue here is that webhooks operate in a headless environment where JavaScript simply won’t execute. I ran into this exact problem last year when building a similar integration. Your PHP script outputs HTML and JavaScript, but there’s no browser present to render or run it when Zapier makes the POST request. Since curl isn’t available, you’ll need to use PHP’s built-in HTTP capabilities. The file_get_contents function with a stream context works well for POST requests. You can construct the context with proper headers and POST data, then make your API calls directly in PHP. I’ve successfully used this approach on shared hosting environments where curl was disabled. Another option is to use a lightweight HTTP library like Guzzle if you can install it via composer.

This is a common misconception about webhook execution environments. When Zapier hits your webhook endpoint, it’s making a direct server-to-server HTTP request. Your PHP script runs on the server and outputs HTML/JavaScript, but there’s absolutely no browser involved to interpret that JavaScript code. I encountered this same confusion when transitioning from frontend development to webhook integrations. The solution is straightforward - move all your API logic into PHP itself. Since curl is unavailable, you can use file_get_contents with stream contexts to make HTTP POST requests. Create the context with method, header, and content parameters, then call your API endpoints directly from PHP. This approach actually performs better than the JavaScript method since you eliminate the browser middleman entirely. The webhook should return a simple success response to Zapier rather than HTML output.

webhooks run server-side but your jquery runs client-side - thats why it doesnt work. zapier hits your webhook but theres no browser to execute the javascript. you need to handle the api calls directly in php using file_get_contents with stream context or look into alternative http libraries if curl isnt available