I\'m using Gravity Forms on a WP site. My forms POST via ajax to Pardot using Pardot form handlers. I am running into an issue where Pardot processes the form 6x, with no ot
What Pardot is suggesting is to create 2 static URLs on your own server that return a simple JSON response.
So for example:
mysite.com/pardot-success
Returns: {"result":"success"}
mysite.com/pardot-error
Returns: {"result":"error"}
You'll then use those two URLs as your success and error redirect URLs for your Pardot form handler settings.
An AJAX request can then be made to your Pardot form handler using JSONP, which will wrap and return the JSON response from one of those URLs (depending on the result).
Your AJAX response data would include your JSON result (success or error).
EDIT: So, after a few days battling with this I'll post my final solution that might help others trying to communicate with Pardot using JSONP. It's a three part problem:
To send form data to the Form Handler you need to URI encode the field names and values.
(Example using JQuery. The '?callback=' is added by ajax() when specifying the dataType: 'jsonp'):
var data = { 'field1' = 'value1', 'field' = 'value2' };
var uriEncodedParams = $.param(data);
var targetUrl = <Pardot Form Handler URL> + '?' + uriEncodedParams;
$.ajax({
url: targetUrl,
dataType: 'jsonp',
jsonpCallback: 'callback'
});
window.callback = function (data) {...}
See @nickjag's answer:
Set the Success Location and Error Location to endpoints on your backend.
As pardot will not forward any of the GET parameters you passed in you'll have to use some defaults on i.e. the callback function name (hence specifying jsonpCallback and not having a success in my request).
I was having problems with console errors (Uncaught SyntaxError: Unexpected token :
) when using:
return "{ 'result' : 'success' }"
as it is a JSON-object and JSONP expects a file with JavaScript. So the format to return should be: return "callback({ 'result' : 'success' })"
And as again Pardot don't forward the GET params, the generated callback function name from JQuery didn't propagate and I couldn't return the correct JavaScript code. Defaulted to use the function name "callback" if none was provided.
Guide for returning JSONP from .NET MVC backend
All the answers here really helped me get going on this solution, but my requirement was to get everything working only through Salesforce. So for anybody looking for the answer using only Salesforce's backend architecture, I hope this helps. I had to structure my Ajax call a little differently to get it working:
var data = { 'username': username };
var uriEncodedParams = $.param(data);
var targetUrl = 'https://my.pardot.url' + '?' + uriEncodedParams;
$.ajax({
url: targetUrl,
type: 'GET',
dataType: 'jsonp',
crossDomain: true
});
window.callback = function(data) {
performMoreActions(data);
}
My server, which is built in Apex (Salesforce's programming language) looks like this:
@RestResource(urlMapping='/pardot/success/*')
global without sharing class Pardot_SuccessProxy {
@HttpGet
global static void Pardot_SuccessProxy() {
RestContext.response.addHeader('Content-Type', 'application/json');
RestContext.response.responseBody = (Blob.valueOf('callback({ "result" : "success" })'));
}
}
I then expose this REST webservice through a Salesforce SITE, at a URL like:
https://my-domain.server-domain.force.com/services/apexrest/pardot/success
https://my-domain.server-domain.force.com/services/apexrest/pardot/error
And in the Pardot Form Handler UI, set the Success Location and Error Location fields to these URLs respectively.
This is very similar to the other answers for this question, but taken from an entirely Salesforce approach. It might be somewhat unrelated the the OPs tech-stack, but it should be helpful for people looking for answers in the future.