如何将对象上下文传递给 jQuery.ajax JSONP 回调?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3399997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass Object context to jQuery.ajax JSONP callback?
提问by TheHobbyist
I run into problems to pass a javascript object context into the callback handler of a JSONP ajax request, when the ajax provider predefines its callback. (Flickr is the service provider).
当 ajax 提供程序预定义其回调时,我遇到了将 javascript 对象上下文传递到 JSONP ajax 请求的回调处理程序的问题。(Flickr 是服务提供商)。
I'll give a simplified example:
我举一个简单的例子:
function Person(anId) { this.name; this.id = anId; var self = this; this.loadName = function() { $.ajax({ url: "jsonp.json", dataType: "jsonp", jsonpCallback : "handleJSON", data : {id: this.id}, context: self, success: function (data) { self.name = data.name; } }); } } var handleJSON = function(data) { console.log("received " + data.name ); } var a = new Person(234); a.loadName(); var b = new Person(345); b.loadName();
The example above works perfectly, the console outputs the line of the handleJSON function. But in this function I don't have a reference to the original object that's calling it. I want that the successfunction is called instead: here I can refer to the selfvariable.
上面的例子完美运行,控制台输出handleJSON函数的行。但是在这个函数中,我没有对调用它的原始对象的引用。我希望调用成功函数:在这里我可以引用self变量。
There are several possible solutions, but I don't get any running.
有几种可能的解决方案,但我没有得到任何运行。
- I'll intercept the callback name that jQuery generates for me and put it as a value in the jsonpCallback parameter. I presume that this function delegates to the specified successfunction, from which I can access this. But I see no way how to obtain that generated name.
- I'll specify the context object like I did in the example. The jQuery docs states that this object would be available to the callback handler. However I cannot find how this context is exposed to e.g. the handleJSONfunction from my example.
- 我将截取 jQuery 为我生成的回调名称,并将其作为值放入 jsonpCallback 参数中。我认为这个函数委托给指定的成功函数,我可以从中访问它。但我看不出如何获得生成的名称。
- 我将像在示例中一样指定上下文对象。jQuery 文档声明该对象可用于回调处理程序。但是,我无法从我的示例中找到此上下文如何暴露给例如handleJSON函数。
Currently I found a solution with the jquery-jsonp from google code. But still I'm very curious how to solve the problem as described at 2. Because I think the jQuery reference states that it could be handled like that. What makes me dependable on less third party libraries.
目前我从谷歌代码中找到了 jquery-jsonp 的解决方案。但是我仍然很好奇如何解决 2 中描述的问题。因为我认为 jQuery 参考声明可以这样处理。是什么让我依赖较少的第三方库。
Can someone show me how to access the context in a JSONP callback handler using jQuery?
有人可以告诉我如何使用 jQuery 访问 JSONP 回调处理程序中的上下文吗?
回答by Guffa
Just leave the jsonpCallback
property alone, and let jQuery create the function that gets called and it's function name. That function will call the success callback that you specify, with the context you specify.
只需保留jsonpCallback
属性,让 jQuery 创建被调用的函数及其函数名称。该函数将使用您指定的上下文调用您指定的成功回调。
function Person(anId) {
this.name;
this.id = anId;
this.loadName = function() {
$.ajax({
url: "jsonp.json",
dataType: "jsonp",
data : {id: this.id},
context: this,
success: function (data) {
this.name = data.name;
}
});
}
}
var a = new Person(234);
a.loadName();
var b = new Person(345);
b.loadName();