简体   繁体   English

Jquery jsonp响应错误 - 未调用回调

[英]Jquery jsonp response error - Callback was not called

I'm trying to get some information from a different domain, the domain allows only jsonp call - others get rejected. 我试图从不同的域获取一些信息,域只允许jsonp调用 - 其他域被拒绝。 How can I get the content instead of execution? 如何获取内容而不是执行? Because I get an error in response. 因为我得到了错误的回应。 I don't need to execute it, I just need it in my script. 我不需要执行它,我只需要在我的脚本中。 In any format (the response is json but js doesn't understand it). 在任何格式(响应是json但js不理解它)。 I can't affect on that domain so it's impossible to change something on that side. 我无法影响该域名,因此无法改变该方面的内容。 Here's my code: 这是我的代码:

$.ajax({
    url: url + '?callback=?',
    crossDomain: true,
    type: "POST",
    data: {key: key},
    contentType: "application/json; charset=utf-8;",
    async: false,
    dataType: 'jsonp',
    jsonp: 'callback',
    jsonpCallback: 'jsonpCallback',
    error: function(xhr, status, error) {
        console.log(status + '; ' + error);
    }
});

window.jsonpCallback = function(response) {
    console.log('callback success');
};

There are a few issues with your $.ajax call. 您的$.ajax调用存在一些问题。

$.ajax({
    url: url + '?callback=?',
    // this is not needed for JSONP.  What this does, is force a local
    // AJAX call to accessed as if it were cross domain
    crossDomain: true,
    // JSONP can only be GET
    type: "POST",
    data: {key: key},
    // contentType is for the request body, it is incorrect here
    contentType: "application/json; charset=utf-8;",
    // This does not work with JSONP, nor should you be using it anyway.
    // It will lock up the browser
    async: false,
    dataType: 'jsonp',
    // This changes the parameter that jQuery will add to the URL
    jsonp: 'callback',
    // This overrides the callback value that jQuery will add to the URL
    // useful to help with caching
    // or if the URL has a hard-coded callback (you need to set jsonp: false)
    jsonpCallback: 'jsonpCallback',
    error: function(xhr, status, error) {
        console.log(status + '; ' + error);
    }
});

You should be calling your url like this: 你应该像这样调用你的网址:

$.ajax({
    url: url,
    data: {key: key},
    dataType: 'jsonp',
    success: function(response) {
        console.log('callback success');
    },
    error: function(xhr, status, error) {
        console.log(status + '; ' + error);
    }
});

JSONP is not JSON. JSONP 不是 JSON。 JSONP is actually just adding a script tag to your <head> . JSONP实际上只是在<head>添加了一个脚本标记。 The response needs to be a JavaScript file containing a function call with the JSON data as a parameter. 响应需要是包含函数调用的JavaScript文件,并将JSON数据作为参数。

JSONP is something the server needs to support. JSONP是服务器需要支持的东西。 If the server doesn't respond correctly, you can't use JSONP. 如果服务器没有正确响应,则无法使用JSONP。

Please read the docs: http://api.jquery.com/jquery.ajax/ 请阅读文档: http//api.jquery.com/jquery.ajax/

var url = "https://status.github.com/api/status.json?callback=apiStatus";
$.ajax({
    url: url,
    dataType: 'jsonp',
    jsonpCallback: 'apiStatus',
    success: function (response) {
        console.log('callback success: ', response);
    },
    error: function (xhr, status, error) {
        console.log(status + '; ' + error);
    }
});

Try this code. 试试这个代码。

Also try calling this url directly in ur browser and see what it exactly returns, by this way You can understand better what actually happens :). 也可以尝试直接在你的浏览器中调用这个url ,看看它究竟返回了什么,通过这种方式你可以更好地理解实际发生的事情:)。

The jsonpCallback parameter is used for specifying the name of the function in the JSONP response, not the name of the function in your code. jsonpCallback参数用于指定JSONP响应中函数的名称,而不是代码中函数的名称。 You can likely remove this; 你可以删除这个; jQuery will handle this automatically on your behalf. jQuery将代表您自动处理此问题。

Instead, you're looking for the success parameter (to retrieve the response data). 相反,您正在寻找success参数(以检索响应数据)。 For example: 例如:

$.ajax({
    url: url,
    crossDomain: true,
    type: "POST",
    data: {key: key},
    contentType: "application/json; charset=utf-8;",
    async: false,
    dataType: 'jsonp',
    success: function(data){
        console.log('callback success');
        console.log(data);
    }
    error: function(xhr, status, error) {
        console.log(status + '; ' + error);
    }
});

You can also likely remove the other JSONP-releated parameters, which were set to jQuery defaults. 您还可以删除其他JSONP相关参数,这些参数设置为jQuery默认值。 See jQuery.ajax for more information. 有关更多信息,请参阅jQuery.ajax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM