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

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

问题描述

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

我举一个简单的例子:

<前>函数人(anId){这个.name;this.id = anId;var self = this;this.loadName = function() {$.ajax({网址:jsonp.json",数据类型:jsonp",jsonpCallback : "handleJSON",数据:{id:this.id},上下文:自我,成功:功能(数据){self.name = data.name;}});}}var handleJSON = 函数(数据){console.log("收到" + data.name );}var a = new Person(234);a.loadName();var b = 新人(345);b.loadName();

上面的例子完美运行,控制台输出handleJSON函数的行.但是在这个函数中,我没有对调用它的原始对象的引用.我希望改为调用 success 函数:在这里我可以引用 self 变量.

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

  1. 我将截取 jQuery 为我生成的回​​调名称,并将其作为值放入 jsonpCallback 参数中.我认为这个函数委托给指定的 success 函数,我可以从中访问它.但我看不出如何获得生成的名称.
  2. 我将像在示例中那样指定上下文对象.jQuery 文档声明该对象可用于回调处理程序.但是我找不到这个上下文是如何暴露的,例如我示例中的 handleJSON 函数.

目前我从谷歌代码中找到了一个带有 jquery-jsonp 的解决方案.但是我仍然很好奇如何解决 2 中描述的问题.因为我认为 jQuery 参考声明可以这样处理.是什么让我依赖较少的第三方库.

有人可以告诉我如何使用 jQuery 在 JSONP 回调处理程序中访问上下文吗?

解决方案

只需保留 jsonpCallback 属性,让 jQuery 创建被调用的函数及其函数名称.该函数将使用您指定的上下文调用您指定的成功回调.

function Person(anId) {这个.name;this.id = anId;this.loadName = function() {$.ajax({网址:jsonp.json",数据类型:jsonp",数据:{id:this.id},上下文:这个,成功:功能(数据){this.name = data.name;}});}}var a = new Person(234);a.loadName();var b = 新人(345);b.loadName();

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



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

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.

Can someone show me how to access the context in a JSONP callback handler using 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();

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

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