如何从函数返回外JSONP调用的结果呢? [英] How to return the result from JSONP call outside the function?
问题描述
我有以下函数的伟大工程,我用JSONP克服跨域,写了一个HTTP模块来改变内容类型,并没有在URL中添加一个回调的名字。
I have the following function which works great, i used JSONP to overcome cross-domain, wrote an http module to alter the content-type and didn't append a callback name in the url.
function AddSecurityCode(securityCode, token) {
var res=0;
$.ajax({ url: "http://localhost:4000/External.asmx/AddSecurityCode",
data: { securityCode: JSON.stringify(securityCode),
token: JSON.stringify(token)
},
dataType: "jsonp",
success: function(json) {
alert(json); //Alerts the result correctly
res = json;
},
error: function() {
alert("Hit error fn!");
}
});
return res; //this is return before the success function? not sure.
}
在RES变量alwayes自带不确定。我不能使用异步=虚假与JSONP。 凭什么我的结果返回功能之外? 我一定要做到这一点对subsequant电话。
the res variable is alwayes comes undefined. and i can't use async=false with jsonp. so how can i return the result to outside the function?? and i sure need to do that for subsequant calls.
请咨询,谢谢。 问题是,我不能在此函数外返回的结果值
Please advice, thanks. The problem is i can't return the result value outside this function
推荐答案
您必须重写你的code流动,使 AddSecurity code
需要回调
参数(即一个函数来运行),您然后调用里面的您的成功回调:
You have to rewrite your code flow so that AddSecurityCode
takes a callback
parameter (i.e. a function to run) that you then invoke inside your success callback:
function AddSecurityCode(securityCode, token, callback) {
$.ajax({
....
success: function(json) {
alert(json); //Alerts the result correctly
callback(json); // HERE BE THE CHANGE
}
....
});
}
这篇关于如何从函数返回外JSONP调用的结果呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!