Jquery jsonp响应错误 - 未调用回调 [英] Jquery jsonp response error - Callback was not called

查看:115
本文介绍了Jquery jsonp响应错误 - 未调用回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从其他域获取一些信息,该域只允许jsonp调用 - 其他域名被拒绝。如何获取内容而不是执行?因为我得到了错误的回应。我不需要执行它,我只需要在我的脚本中。在任何格式(响应是json但js不理解它)。
我不能影响该域名,因此无法改变该方面的内容。
这是我的代码:

I'm trying to get some information from a different domain, the domain allows only jsonp call - others get rejected. 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). 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');
};


推荐答案

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

JSONP is not JSON. JSONP is actually just adding a script tag to your <head>. The response needs to be a JavaScript file containing a function call with the JSON data as a parameter.

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

JSONP is something the server needs to support. If the server doesn't respond correctly, you can't use JSONP.

请阅读文档: http://api.jquery.com/jquery.ajax/

这篇关于Jquery jsonp响应错误 - 未调用回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆