Simulate a JSONP response with JavaScript URLs

前端 未结 3 1053
南方客
南方客 2020-12-16 17:12

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

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-16 17:54

    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.

提交回复
热议问题