$(阿贾克斯)包装的jQuery - 传递参数给与会代表 [英] $.(ajax) wrapper for Jquery - passing parameters to delegates
问题描述
我用$。(AJAX)功能广泛我的应用程序调用ASP.net Web服务。我想,以集中所有的Ajax调用编写一个包装。我发现了几个简单的解决方案,但没有解决参数传递给代表,例如一个问题,如果我有:
I use $.(ajax) function extensively in my app to call ASP.net web services. I would like to write a wrapper in order to centralize all the ajax calls. I found few simple solutions, but none address an issue of passing parameters to delegates, for example, if i have:
$.ajax({
type: "POST",
url: "http://localhost/TemplateWebService/TemplateWebService/Service.asmx/GetFoobar",
data: jsonText,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
var results = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
OnSuccess(results, someOtherParam1, someOtherParam2);
},
error: function(xhr, status, error) {
OnError();
}
});
该包装这一号召就必须有从包装变量为一个通用的数组传递someOtherParam1,someOtherParam2到的onSuccess代表......除此之外的方式,我想不出其他的解决方案。
The wrapper to this call would have to have the way to pass someOtherParam1, someOtherParam2 to the OnSuccess delegate...Aside from packing the variables into a generic array, I can't think of other solutions.
你们是怎么解决这个问题?
How did you guys address this issue?
推荐答案
我有同样的问题。我周围的办法是包括数据变量的参数,然后将其拉出this.data对成功/错误/完成。 this.data看起来类似foo = 1&安培;巴= 2及。巴兹= 3时,它回来了,所以你需要解析的参数了
I had the same issue. My way around it was to include the parameter in the data variable and then pull it out of this.data on success/error/complete. this.data will look like foo=1&bar=2&baz=3 when it comes back so you will need to parse the parameters out.
$.ajax({
url: 'blah.html',
data: {foo:1,someOtherParam1:2,someOtherParam2:3},
success: function(data) {
var params = this.data.split("&");
OnSuccess(results, params[1].split("=")[1], params[2].split("=")[1]);
}
});
请确保你没有得到this.data困惑的是与成功函数返回的数据变量...
Make sure you don't get this.data confused with the data variable that is returned with the success function...
这篇关于$(阿贾克斯)包装的jQuery - 传递参数给与会代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!