如何将Object上下文传递给jQuery.ajax JSONP回调? [英] How to pass Object context to jQuery.ajax JSONP callback?

查看:201
本文介绍了如何将Object上下文传递给jQuery.ajax JSONP回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当ajax提供者预定义它的回调时,我遇到了一个问题,将一个javascript对象上下文传递到一个JSONP ajax请求的回调处理程序中。 (Flickr是服务提供商)。

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).

我将给出一个简化的例子:

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();



上面的例子完美地工作,控制台输出handleJSON函数的行。但在这个函数中,我没有引用调用它的原始对象。我想要调用成功函数:这里我可以参考 self 变量。

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 success function is called instead: here I can refer to the self variable.

有几种可能的解决方案,但我没有任何运行。

There are several possible solutions, but I don't get any running.


  1. 我将拦截jQuery为我生成的回​​调名称,并将其作为jsonpCallback参数中的值。我假设这个函数委托给指定的成功函数,我可以从中访问它。但我看不到如何获取生成的名称。

  2. 我将指定上下文对象,就像我在示例中做的。 jQuery文档声明此对象将可用于回调处理程序。然而,我不能找到这个上下文如何暴露于例如。

  1. 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 success function, from which I can access this. But I see no way how to obtain that generated name.
  2. 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 handleJSON function from my example.

目前我发现了一个使用google代码的jquery-jsonp的解决方案。但仍然我很好奇如何解决问题,如描述在2.因为我认为jQuery参考说明它可以这样处理。什么让我可靠的第三方图书馆。

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回调处理程序中的上下文?

Can someone show me how to access the context in a JSONP callback handler using jQuery?

推荐答案

只需留下 jsonpCallback 属性,让jQuery创建被调用的函数,它就是函数名。该函数将使用您指定的上下文调用您指定的成功回调。

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.

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();

这篇关于如何将Object上下文传递给jQuery.ajax JSONP回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆