dongmi5015
dongmi5015
2016-05-30 13:04

首次使用AJAX从PHP获取数据

So i simply use this updateData function to get values from my php file it works after the first interval, but I would like to get first data after the page loads so I've tried doing var

var dataset=[];
updateData();

Before the intervals starts but dataset is still not updated immidiately I get empty array []

function updateData(){
    $.ajax({
            type: 'POST',
            url: 'parser.php',         
            dataType: 'json',
            cache: false,
            success: function(result) {
                dataset = result;
            },
    });

}
<?php 
    $datasetJSON = array();

    for($i=0;$i<7;$i++){
        $datasetJSON[] = rand(1,1000);
    }
    echo json_encode($datasetJSON);
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • douzouchang7448 douzouchang7448 5年前

    I think your problem is understanding that AJAX is not synced!

    That means your code will keep running and not wait for the AJAX call to return. You could think of AJAX calls as threads that will run seperately of your main thread.

      var dataset=[];
      updateData();
      console.log(dataset); // will execute immediately!
      function updateData(){
          $.ajax({
                  type: 'POST',
                  url: 'parser.php',
                  dataType: 'json',
                  cache: false,
                  success: function(result) {
                      dataset = result;
                      console.log(dataset); // now it's updated
                  },
          });
      }
    

    You could call AJAX synced:

      var dataset=[];
      updateData();
      console.log(dataset); // now it's updated
      function updateData(){
          $.ajax({
                  type: 'POST',
                  url: 'parser.php',
                  dataType: 'json',
                  cache: false,
                  async: false, // <-- This will sync the AJAX call (and freeze the browser)
                  success: function(result) {
                      dataset = result;
                      console.log(dataset); // now it's updated
                  },
          });
      }
    

    However, it defeats the purpose of AJAX. You should learn how the deal with callback functions.

    点赞 评论 复制链接分享
  • douzhi7082 douzhi7082 5年前

    Add your ajax call on document ready time like this

    <script type="text/javascript">
    jQuery(document).ready(function(){
    
            tableselect = jQuery(this).val() ;
                var data_pro = {
                        action: 'update_record',
                        tableselect: tableselect
                        };
    
                        jQuery.ajax({
                                    type: "POST",
                                    dataType : 'html',
                                    url: ajaxurl,
                                    data: data_pro,
                                      success: function(data){
    
                                        }
                                });
    
        });
    </script>
    
    点赞 评论 复制链接分享
  • doute3621 doute3621 5年前

    just detect the page loaded or not then call the function like this. is this you want?

    jQuery(window).load(function () {
        updateData();
    });
    

    Hope this will help you.

    点赞 评论 复制链接分享