简体   繁体   English

通过参数PHP将变量传递给AJAX函数

[英]Passing variables to an AJAX function through the parameters PHP

I'm trying to pass 4 variables through the parameters of the ajax function, but it's not accepting it for some reason. 我试图通过ajax函数的参数传递4个变量,但是由于某种原因它不接受它。 The variables I'm trying to pass are the url, action, id, and the timeout in milliseconds. 我要传递的变量是url,action,id和超时(以毫秒为单位)。 If anyone know what I'm doing wrong, please correct me. 如果有人知道我在做什么错,请纠正我。 Thank you. 谢谢。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script type = "text/javascript"> 

function myAjax(url,action,id,timeout) {
      $.ajax({
           type: "POST",
           url: url,
           data:{action: action},
           error: function(xhr,status,error){alert(error);},
           success:function(data) {
             document.getElementById( id ).innerHTML = data;
             setTimeout(myAjax, timeout);
           }

      });

}

</script>

<body onload="myAjax('testing.php','call_this','my_div',2000)">

<div id="my_div"></div>

</body>

Here's the working version of the code, without passing the variables through the parameters, and instead having them inside the function itself: 这是代码的工作版本,没有通过参数传递变量,而是将变量包含在函数本身中:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script type = "text/javascript"> 

function myAjax() {
      $.ajax({
           type: "POST",
           url: 'testing.php',
           data:{action: 'call_this'},
           error: function(xhr,status,error){alert(error);},
           success:function(data) {
             document.getElementById( 'my_div' ).innerHTML = data;
             setTimeout(myAjax, 2000);
           }

      });

}

</script>

<body onload="myAjax()">

<div id="my_div"></div>

</body>

Re-write the setTimeout() call like so: 像这样重写setTimeout()调用:

setTimeout(function() { 
    myAjax(url,action,id,timeout); 
}, timeout);

The second time it is called (from your success handler), you are not passing any parameters to myAjax() so it will not have any arguments when it is invoked the second time. 第二次调用(从success处理程序中),您没有将任何参数传递给myAjax()因此,第二次调用它时,它将没有任何参数。

There are a couple ways you can fix that. 有几种方法可以解决此问题。 One way it to just copy the arguments when you call myAjax(...) from within: 一种从内部调用myAjax(...)时仅复制参数的方法:

function myAjax(url,action,id,timeout) {
      $.ajax({
           type: "POST",
           url: url,
           data:{action: action},
           error: function(xhr,status,error){alert(error);},
           success:function(data) {
             document.getElementById( id ).innerHTML = data;
             setTimeout(function() {
                 myAjax(url, action, id, timeout);
             }, timeout);
           }
      });
}

But, you could also make an inner function and then just reference the arguments from the closure like this: 但是,您也可以创建一个内部函数,然后像这样从闭包中引用参数:

function myAjax(url,action,id,timeout) {
      function run() {
          $.ajax({
               type: "POST",
               url: url,
               data:{action: action},
               error: function(xhr,status,error){alert(error);},
               success:function(data) {
                 document.getElementById( id ).innerHTML = data;
                 setTimeout(run, timeout);
               }
          });
      }
      // run the ajax function the first time
      run();
}

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

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