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

时间:2015-06-04 00:14:36

标签: javascript php jquery ajax

我试图通过ajax函数的参数传递4个变量,但由于某种原因它不接受它。我试图传递的变量是url,action,id和超时(以毫秒为单位)。如果有人知道我做错了什么,请纠正我。谢谢。

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

这是代码的工作版本,不通过参数传递变量,而是将它们放在函数本身中:

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

2 个答案:

答案 0 :(得分:3)

重新编写- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"SimpleTableCell"; SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; } NSDictionary *dictObject = [places objectAtIndex:indexPath.row]; cell.nameLabel.text = [dictObject valueForKey:@"PlaceTitle"]; NSURL *url = [NSURL URLWithString:@"http://images1.fanpop.com/images/image_uploads/Mario-Kart-Wii-Items-mario-kart-1116309_600_600.jpg"]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *image = [UIImage imageWithData:data]; cell.thumbnailImageView.image = image; return cell; } 这样的电话:

setTimeout()

答案 1 :(得分:1)

第二次调用它(来自你的success处理程序),你没有将任何参数传递给myAjax(),所以当它第二次被调用时它将没有任何参数。

有几种方法可以解决这个问题。从内部调用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);
           }
      });
}

但是,您也可以创建一个内部函数,然后只需引用闭包中的参数:

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