jQuery的全局变量

Just got the FooTable responsive table plugin to work. Now I am trying to setup a PHP script to pull PostgreSQL and return a JSON encoded array.

Everything is working fine so far. I am really close to making this jQuery script work, but I'm not sure why my variables are not passing along.

Here is the script:

  var columns_json;
  var rows_json;

  jQuery(function($){

  $.ajax(
    {
      type: "POST",
      dataType:"JSON",
      url: "a.php",
      data: {action: 'test'},

      success: function(data)
        {
          columns_json = data[0];
          rows_json = data[1];

          console.log(columns_json);
          console.log(rows_json);
        },

      failure: function(data) 
        {
          alert("Something went wrong");
        }
    });

    $('.table').footable(
    {

      "paging": {"enabled": true},

      "filtering": {"enabled": true},

      "sorting": {"enabled": true},

      "columns": columns_json,

      "rows": rows_json                   

      });
});

If I look at my console, I can even see the two data arrays returned correctly... I even tried to output the data to make sure it was correct (no issue there):

console.log(JSON.stringify(columns_json))

So what I am not understanding about jQuery is: When I update the variables I declared at the top of the scripts from within the $.ajax function, why are the JSON arrays not available at the $('.table').footable( function?

Admitting I've been only playing with jQuery for a little over a month so this is all new to me.

I did find one workaround to get this to work and that was the suggestion on this Post. I modified my script and got it to work. However the console throws a warning:

"Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.".

Like always, any thoughts and suggestions are much appreciated.

查看全部
weixin_33749242
weixin_33749242
2016/08/29 07:32
  • json
  • php
  • ajax
  • footable
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信

1个回复