将数据从$ .post设置为var


                    

代码:</ p>

  $ .post('script.php',{value:value},function(data)
  {
         var Aquaman = ???;
  });
</ code> </ pre>

现在我的问题是,是否要将数据值设置为Aquaman变量,该怎么办?</ p>
     </ div>

展开原文

原文

The code:

 $.post('script.php',{value:value},function(data)
 {
        var Aquaman= ???;
 });

Now my question is if I want to set the data value into the Aquaman variable, how do I do it?

3个回答


data </ code>的值分配给 Aquaman </ code>的过程很容易,如下所示:</ p>

  $。post('script.php',{value:value},function(data){
     var Aquaman =数据;
});
</ code> </ pre>

但是,由于变量范围</ a>如果您需要在 $。post </ code>回调之外使用此值,则需要在此函数之外定义 Aquaman </ code>。 例如:</ p>

  //某些代码
$ .post(...,function(data){
   var Aquaman =数据;
});

//这里没有定义Aquaman。
</ code> </ pre>

但是:</ p>

  var Aquaman;
$ .post(...,function(data){
     海王=数据;
});
// Aquaman存在(但请注意,AJAX调用使此值不可访问
//,直到执行完回调。)
</ code> </ pre>
     </ div>

展开原文

原文

Assigning the value of data to Aquaman is as easy as the following:

$.post('script.php',{value:value},function(data){
    var Aquaman= data;
});

However, because of variable scope if you need this value outside of the $.post callback you need to define Aquaman outside of this function. For example:

//Some Code
$.post(...,function(data){
  var Aquaman = data;
});

// Aquaman is undefined out here.

However:

var Aquaman;
$.post(...,function(data){
    Aquaman = data;
});
// Aquaman exists (but be careful, AJAX calls make this value not accessible
// until the callback has been executed.)


欢迎来到异步逻辑的美好世界。 </ p>

您需要做的只是 var Aquaman = data; </ code>-但是,该变量将仅存在于该匿名函数中。 </ p>

您可以通过以通常的方式分配全局变量来解决此问题:</ p>

  var Aquaman;
  $ .post('script.php',{value:value},function(data){
       Aquaman =数据;
  });
</ code> </ pre>

但是,直到您的AJAX调用完成后,才会设置此全局变量。 例如,如果 Aquaman </ code>正在被其他AJAX调用或用户事件调用,则这可能是理想的。 但是,以下代码将无法正常工作:</ p>

  var Aquaman;
  $ .post('script.php',{value:value},function(data){
       Aquaman =数据;
  });
  console.log(Aquaman); //不显示任何内容
</ code> </ pre>

...因为 console.log()</ code>立即运行,并且在 $。post()</ code>完成之前运行。 </ p>

防止 </ em>的唯一方法是进行同步 AJAX调用:< / p>

  $。ajax({url:'script.php',data:{value:value},async:false},function(data){...});
</ code> </ pre>

...但是,这通过强制脚本停止并等待回调完成而使AJAX的大多数优点无效。 在几乎所有情况下,最好让您的AJAX保持异步并处理回调中的 data </ code>。</ p>
     </ div>

展开原文

原文

Welcome to the wonderful world of asynchronous logic.

All you need to do is var Aquaman = data; -- however, that variable will ONLY exist inside that anonymous function.

You can get around this by assign global variables in the usual manner:

 var Aquaman;
 $.post('script.php',{value:value},function(data) {
      Aquaman= data;
 });

However, this global variable won't be set until after your AJAX call is completed. This may be desirable if, for instance, Aquaman is being called by other AJAX calls or user events. However, the following code won't work as you might expect:

 var Aquaman;
 $.post('script.php',{value:value},function(data) {
      Aquaman= data;
 });
 console.log(Aquaman); // displays nothing

...because console.log() is run immediately and before the $.post() is completed.

The only way to prevent that is to make a synchronous AJAX call:

$.ajax({url: 'script.php', data: {value:value}, async: false}, function(data) {...});

...but this nullifies most of the advantages of AJAX by forcing your script to stop and wait for the callback to complete. In almost all cases, it's better to let your AJAX remain asynchronous and deal with the data inside the callback.


要简化一个基本问题,请在全局范围内声明Aquaman变量:</ p>

  var Aquaman;

函数something()
{
      $ .post('script.php',{value:value},function(data)
      {
          Aquaman =数据;
      });
}
</ code> </ pre>

您可以找到更好的方法,请参见 。</ p>
     </ div>

展开原文

原文

To make it simple to a so basic question, declare your Aquaman variable in global scope:

var Aquaman;

function something()
{
     $.post('script.php',{value:value},function(data)
     {
         Aquaman= data;
     });
}

You can find better way to do this, see there.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问