I'm working on a simple plugin to facilitate front-end, anonymous lead generation forms submitted to Salesforce via cURL to it's Web-to-Lead servlet. The logic and data transfer are working (debug showing positive receipt of the query from the servlet), but no matter what the :success function on the $.ajax call isn't triggered.
I'm getting a 200 OK back from the cURL, so my assumption is that the :error function is being triggered because the result isn't properly formatted JSON. I'm calling the returnJson function from the baseController, however all I can see in the response object is "responseText" with the raw HTML of the servlet response.
Controller PHP
protected $allowAnonymous = array('actionPostLead');
public function actionPostLead()
{
$this->requireAjaxRequest();
// Initialize the $kv array and query var for later use
$kv = array();
$query_string = '';
//If there are POST variables
if ($_POST) {
// For each POST variable as $name_of_input_field => $value_of_input_field
foreach ($_POST as $key => $value) {
// Set array element for each POST variable (ie. first_name=Arsham)
$kv[] = stripslashes($key).'='.stripslashes($value);
}
// Create a query string with join function separted by &
$query_string = join('&', $kv);
}
// Check to see if cURL is installed ...
if (!function_exists('curl_init')) {
die('Sorry cURL is not installed!');
}
// The original form action URL from Step 2 :)
$url = 'https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
// Open cURL connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($kv));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
// Execute SalesForce web to lead PHP cURL
$result = curl_exec($ch);
// close cURL connection
curl_close($ch);
if ($result) {
$this->returnJson(array('success' => true));
} else {
$this->returnErrorJson($e->getMessage());
}
}
Javascript
$.ajax({
type: 'POST',
url: '/',
data: data,
dataType: 'json',
success: function(response) {
formBlock.removeClass('submitting');
var successMessage = $('.formBlock.on').data('success-message');
showFormSuccessBanner(successMessage);
hideForm(id);
},
error: function(err) {
console.log("Error");
console.log(err);
formBlock.removeClass('submitting');
}
});
I think I'm close, but I've been digging for several hours and can't figure out what I'm doing wrong with the response.
dataType: 'json'
from your ajax I never used that in my craft projects, maybe this is key(?) If not, could you please make a screenshot from your network tab with the exact response from the server?