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.