简体   繁体   English

带有JSONP的AJAX返回错误回调未定义

[英]AJAX with JSONP returns error Callback is Undefined

Here is my ajax call. 这是我的ajax电话。 I know the headers are correct because when I access the url directly it gives me something like this: jsonpCallback({"id":"274"}) 我知道标题是正确的,因为当我直接访问url时它会给我这样的东西: jsonpCallback({"id":"274"})

But when I make the ajax call - it is saying Uncaught ReferenceError: jsonpCallback is not defined 但是当我进行ajax调用时 - 它是说Uncaught ReferenceError: jsonpCallback is not defined

$.ajax({
    url: 'http://localhost:9000/product/rest/company?' + $('form').serialize(),
    type: 'GET',
    crossDomain: true, // enable this
    dataType: 'jsonp',
    jsonpCallback: 'callback',
    success: function(data) {
        console.log(data);
    },
    error: function(err) {
        console.log(err);
    }
}).then(function(data) {
    console.log(data);
});

What am I doing wrong in this call? 我在这次电话会议中做错了什么?

The server is giving back the JSONP resource using the wrong callback function name. 服务器使用错误的回调函数名称返回JSONP资源。

You wrote that the response from the server is something like: 您写道服务器的响应是这样的:

jsonpCallback({"id":"274"})

The JSON data is wrapped into the padding function jsonpCallback whose name is not matching with the parameter specified in the Ajax request. JSON数据被包装到填充函数jsonpCallback ,该函数的名称与Ajax请求中指定的参数不匹配。 It should be: 它应该是:

callback({"id":"274"})

in fact callback is the name passed as jsonpCallback option in the jQuery AJAX call 事实上, callback是在jQuery AJAX调用中作为jsonpCallback选项传递的名称

jsonpCallback: 'callback',

Editing the server side script that generates the response to apply a proper callback name will fix things. 编辑生成响应的服务器端脚本以应用适当的回调名称将解决问题。

Alternatively you can fix things on "the other side" by making the ajax call parameter matching with the function name in the response: 或者,您可以通过使ajax调用参数与响应中的函数名称匹配来修复“另一方”:

jsonpCallback: 'jsonpCallback',


The above is the "quick fix". 以上是“快速修复”。

However it is strongly reccomended that you don't specify a custom callback name (using the parameter jsonpCallback ). 但是强烈建议您不要指定自定义回调名称 (使用参数jsonpCallback )。

Instead let jQuery generate a random callback name for each request. 相反,让jQuery为每个请求生成一个随机回调名称。

The server side script will then parse the GET parameter callback and use the name passed for the callback function name that wraps the JSON data. 然后,服务器端脚本将解析GET参数callback并使用为包装JSON数据的回调函数名称传递的名称。

For example if you make a request like: 例如,如果您提出以下请求:

http://localhost:9000/product/rest/company?param=123

jQuery will add a parameter to the query string like this: jQuery会在查询字符串中添加一个参数,如下所示:

http://localhost:9000/product/rest/company?param=123&callback=jQuery1520422276

(it actually uses a longer callback name) (它实际上使用更长的回调名称)

The server should parse callback and build up the response using the value passed as the padding function that wraps the JSON returned data: 服务器应该解析callback并使用作为包装JSON返回数据的填充函数传递的值来构建响应:

jQuery1520422276({"id":"274"})

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

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