[英]Response/callback for JSONP using jQuery $.ajax not working
I am unable to catch response from C# to jQuery using $.ajax
. 我无法使用
$.ajax
捕获C#对jQuery的响应。 I get an error "SCRIPT ERROR"
. 我收到错误
"SCRIPT ERROR"
。 How can I catch response using JSONP? 如何使用JSONP捕获响应? This is what i am using:
这就是我正在使用的:
$.ajax({
async: true,
context: mrq,
cache: false,
type: "GET",
url: MYURL,
crossDomain: true,
dataType: 'jsonp',
data: MYDATA,
processData: false,
jsonp: "jsonREQ",
jsonpCallback: "onJSONPsuccess",
success: function (jsonText, textStatus) {}
});
As far as I understand, dataType: 'jsonp'
means that as soon as it's returned it's put into the callback function as an argument. 据我了解,
dataType: 'jsonp'
意味着一旦返回,它将作为参数放入回调函数中。 So, I'd try this: 因此,我会尝试这样做:
onJSONPsuccess = function(response) {
// do something with response, e.g.
results = response["results"]
}
$.ajax({
crossDomain: true,
dataType: 'jsonp',
data: { /*params you're sending in*/ },
jsonp: "jsonREQ",
jsonpCallback: "onJSONPsuccess",
success: onJSONPsuccess
});
You say the server side is C# - are you using WCF? 您说服务器端是C#-您在使用WCF吗? There's a great article here about it: http://bendewey.wordpress.com/2009/11/24/using-jsonp-with-wcf-and-jquery/
这里有一篇很棒的文章: http : //bendewey.wordpress.com/2009/11/24/using-jsonp-with-wcf-and-jquery/
Basically you need to set up WCF (or whatever server-side code you're using) to return the json wrapped in a call to your callback function. 基本上,您需要设置WCF(或您正在使用的任何服务器端代码)以返回包装在对回调函数的调用中的json。
However, with jquery, you can simply add "?Callback=?" 但是,使用jquery,您可以简单地添加“?Callback =?” to your URL, change the dataType to 'jsonp', and forget about the rest of that stuff.
到您的URL,将dataType更改为'jsonp',然后忽略其余的内容。 You don't need the jsonp or jsonpCallback options set.
您不需要设置jsonp或jsonpCallback选项。
In contrast to a json request, the jsonp request will return your data not wrapped in a 'd' property, so your call back function is: 与json请求相反,jsonp请求将返回未包装在'd'属性中的数据,因此您的回调函数为:
function(data) { var a = data.myProperty ... }
rather than 而不是
function(data) { var a = data.d.myProperty ... }
and the whole method is along the lines of: 整个方法大致如下:
var url = configuration.serviceUrl + "/" + method + "?callback=?";
var options = {
type: 'GET',
url: url,
data: args,
dataType: "jsonp",
contentType: "application/json",
success: function(data) {
if (callback != null) callback(data);
}
};
if (typeof errorCallback != 'undefined')
options.error = errorCallback;
$.ajax(options);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.