1

我正在发送一个 ajax 请求以使用 jsonp 从 salesforce 检索数据,但 jsonp 回调数据返回未定义

我的ajax请求是,

$j.ajax({
            type: "POST",
            async: this.asyncAjax,
            url: "https://na14.salesforce.com/services/data/v26.0/
                      sobjects/customobject?callback=mycallback",
            contentType: 'application/json',
            cache: false,
            processData: false,
            data: payload,
            jsonp: false,
            jsonpCallback: function(data, textStatus, jqXHR){
                    alert('Data is '+data);
                    alert('Text status is '+textStatus);
                    alert('jqXHR is '+jqXHR);
            },
            error: (!this.refreshToken || retry ) ? error : function(jqXHR, textStatus, errorThrown) {
                if (jqXHR.status === 401) {
                    that.refreshAccessToken(function(oauthResponse) {
                        that.setSessionToken(oauthResponse.access_token, null,
                        oauthResponse.instance_url);
                        that.ajax(path, callback, error, method, payload, true);
                    },
                    error);
                } else {
                    error(jqXHR, textStatus, errorThrown);
                }
            },
            dataType: "jsonp",
            beforeSend: function(xhr) {
                if (that.proxyUrl !== null) {
                    xhr.setRequestHeader('SalesforceProxy-Endpoint', url);
                }
                xhr.setRequestHeader(that.authzHeader, "OAuth " + that.sessionId);
                xhr.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + that.apiVersion);

                if (that.userAgentString !== null) {
                    xhr.setRequestHeader('User-Agent',that.userAgentString);
                }
            }
        });

但是,我得到的回应是,

Data is undefined.
Text status is undefined.
jqXhr is undefined.

我该如何解决这个问题?该ajax请求的格式是否正确?

谢谢

4

1 回答 1

0

您正在手动设置发送到 URL 的回调参数的名称,但没有调用函数mycallback

您正在设置jsonpCallback一个功能。jQuery 将获取该函数的返回值并将其作为callback参数发送(如果您让它发送该参数)。

做 JSONP的正确方法是:

$j.ajax({
            type: "POST",
            async: this.asyncAjax,
            url: "https://na14.salesforce.com/services/data/v26.0/sobjects/customobject",
            contentType: 'application/json',
            cache: false,
            processData: false,
            data: payload,
            dataType: 'jsonp',
            success: function(data, textStatus, jqXHR){
                    alert('Data is '+data);
                    alert('Text status is '+textStatus);
                    alert('jqXHR is '+jqXHR);
            },
于 2013-01-14T15:23:54.420 回答