Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
753 views
in Technique[技术] by (71.8m points)

ajax - Simulate a JSONP response with JavaScript URLs

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 other errors. Research indicates that this is because Pardot does not support CORS or JSONP, and thus gets stuck in a loop when using ajax to submit. It's processing the submission but never "finishing" when the form handler's Success URL is set as referring URL. It tries 6x before giving up, processing the submitted data and sending new prospect notification emails each time.

Pardot help docs suggest the following solution:

It is possible to simulate a JSONP response by setting the Success and Error URLs for the form handler to be JavaScript URLs that execute Success and Error callbacks, respectively.

I'm not totally sure what this means or how to approach it. I've done some stackoverflowing and googling, but I can't seem to wrap my head around how to do it. Can someone help clarify this concept or point me in the right direction?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  1. Send a JSONP request to a Pardot Form Handler
  2. Redirect Pardot Form Handler success/error to your own server
  3. Return JSONP from your server

Send a JSONP request to Pardot From Handler

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) {...}

Redirect Pardot From Handler success/error to your own server

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).

Return JSONP from your server

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...