简体   繁体   English

将调用对象访问到ajax响应中(不是ajax调用)

[英]Accessing the calling object into ajax response… (not the ajax call)

I have an object of type Application (defined by me). 我有一个类型为Application(由我定义)的对象。 Whenever an object of this type is created, it automatically loads a php file say "start.php" using jquery ajax and assign the response to a div say "Respo". 每当创建这种类型的对象时,它都会使用jquery ajax自动加载一个名为“ start.php”的php文件,并将响应分配给一个名为“ Respo”的div。 Now what i want is to access the Application object from that Respo div. 现在,我要从该Respo div访问Application对象。 Unfortunately, i have no clue how to do this... in my ajax call: 不幸的是,在我的ajax调用中,我不知道如何执行此操作:

  function Application(options)
  {
  .......

  var appObj=this;
  $.ajax({
   url:appObj.location,      //Already defined
   success:function(data)
   {
   $("#respo").html(data);
   }
   });
  }

Now in my Respo division i want to access that Application object... I tried: alert(this) but it resulted in an object of DOMWindow... i tried editing success function as: 现在在我的Respo部门中,我想访问该Application对象...我尝试了:alert(this),但是它导致了DOMWindow对象...我尝试将成功函数编辑为:

  function Application(options)
  {
        .......

        var appObj=this;
        $.ajax({
         url:appObj.location,      //Already defined
         success:function(data)
         {
             $("#respo").html("<script type='text/javascript'>var Self="+appObj+"</script>");
             $("#respo").html(data);
         }
         });
  }

But i ended nowhere. 但是我无处可去。 :( Although if i assign "var Self='nishchay';" then alerting Self from start.php gives nishchay but i am not able to assign the calling object of Application type to the Self variable. It is the only way I cud think of. :\\ :(尽管我分配了“ var Self ='nishchay';”,然后从start.php发出Self警报,但仍无法将Application类型的调用对象分配给Self变量。这是我认为的唯一方法的:\\

Please help me... actually my object has some editing functions to control itself - its look and feel and some other options. 请帮助我...实际上,我的对象具有一些编辑功能来控制自身-它的外观和感觉以及其他一些选项。 I want the code loaded by object to control the object itself. 我希望对象加载的代码可以控制对象本身。

Please help me.. Thanks in advance. 请帮助我..在此先感谢。 Nishchay Nishchay

You can pass "this" as the "context" property (jQuery1.4) in the $.ajax then you can access that inside the "success" callback simply as "this", below is what jQuery doc says: 您可以在$ .ajax中将“ this”作为“ context”属性(jQuery1.4)传递,然后可以在“成功”回调内部以“ this”的形式简单地访问它,以下是jQuery Doc所说的:

This object will be made the context of all Ajax-related callbacks. 该对象将成为所有与Ajax相关的回调的上下文。 For example specifying a DOM element as the context will make that the context for the complete callback of a request 例如,将DOM元素指定为上下文将使该上下文用于请求的完整回调

Here is code example: 这是代码示例:

function Application(options)
  {
        .......

        var appObj=this;
        $.ajax({
         url:appObj.location,      //Already defined
         context: this,
         success:function(data)
         {
             console.log( this ); // will be pointing to the object that you passed as the value of the "context" property

             // YOU SHOULD NOT BE USING THINGS LIKE BELOW
             //$("#respo").html("<script type='text/javascript'>var Self="+appObj+"</script>");
             $("#respo").html(data);
         }
         });
  }

Hope it helps. 希望能帮助到你。

var appObjPool = {};
var appObjID = 'xxx';

  function Application(options)
  {
        .......
appObjPool[appObjID] = appObj;
        var appObj=appObjPool[appObjId];

        $.ajax({
         url:appObj.location,      //Already defined
         success:function(data)
         {
             $("#respo").html("var Self=appObjPool['"+appObjID+"']");
             $("#respo").html(data);
         }
         });
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM