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

Javascript Jquery jsonp响应错误-未调用回调,javascript,json,jquery,jsonp,Javascript,Json,Jquery,Jsonp,我试图从另一个域获取一些信息,该域只允许jsonp调用,其他域则被拒绝。如何获取内容而不是执行?因为我得到了一个错误的回应。我不需要执行它,我只需要在脚本中使用它。任何格式(响应是json,但js不理解)。 我不能影响那个领域,所以不可能改变那个方面的东西。 这是我的密码: $.ajax({ url: url + '?callback=?', crossDomain: true, type: "POST", data: {key: key}, conten

我试图从另一个域获取一些信息,该域只允许jsonp调用,其他域则被拒绝。如何获取内容而不是执行?因为我得到了一个错误的回应。我不需要执行它,我只需要在脚本中使用它。任何格式(响应是json,但js不理解)。 我不能影响那个领域,所以不可能改变那个方面的东西。 这是我的密码:

$.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');
};

jsonpCallback
参数用于指定JSONP响应中函数的名称,而不是代码中函数的名称。你可以删除这个;jQuery将代表您自动处理此问题

相反,您要查找
success
参数(以检索响应数据)。例如:

$.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);
    }
});

您还可以删除其他JSONP相关参数,这些参数设置为jQuery默认值。有关更多信息,请参阅。

您的
$.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);
    }
});
您应该这样调用您的url:

$.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不是JSON。JSONP实际上只是在
中添加了一个脚本标记。响应需要是一个JavaScript文件,其中包含以JSON数据为参数的函数调用

JSONP是服务器需要支持的东西。如果服务器没有正确响应,就不能使用JSONP

请阅读以下文件:

试试这个代码


还可以尝试直接在您的浏览器中调用它,查看它到底返回了什么,这样您可以更好地了解实际发生的情况:)。

服务器的响应是什么这里还有一些问题。JSONP无法发布。
contentType
用于请求,而不是响应(删除它)
async:false通常是一个坏主意,它不适用于JSONP。这个答案解决了他的问题,但使问题解决方案非常抽象,并不能帮助其他人将其应用于他们的情况。非常感谢您的帮助!
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);
    }
});