如何将ajax JSON对象存储到要在函数外部访问的全局变量中

时间:2022-09-18 14:03:18

How do I store ,success function returned data into global variable.

如何存储,成功函数将返回的数据存储到全局变量中。

$("form").on("submit", function () {
    var data = {
        "action": "test"
    };
    data = $(this).serialize() + "&" + $.param(data);
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "ajax2.php",
        data: data,
        success: function (data) {
            //How to store the above data into a global variable?
            $("#main_content").slideUp("normal", function () {
                for (i = 0; i < data.length; i++) {
                    $(".the-return").append("<div class='inside_return'>Name:" 
                            + data[i].name + "<br/>Id:" 
                            + data[i].id + "<br/>Pricing:" 
                            + data[i].rate + "<br/>Postcode:" 
                            + data[i].postcode + "<br/>Reputation:" 
                            + data[i].reputation + "<br/>Review Plus:" 
                            + data[i].plus + "<br/>Review Negative:" 
                            + data[i].neg + "<br/><h1>Availability</h1>Week Morning:" 
                            + data[i].weekM + "<br/>Week Afternoon:" 
                            + data[i].weekA + "<br/>Week Evening:" 
                            + data[i].weekE + "<br/>Weekend Morning:" 
                            + data[i].endM + "<br/>Weekend Afternoon:" 
                            + data[i].endA + "<br/>Week Evening:" 
                            + data[i].endE + "</div>");
                    //alert(data[i].name) 
                }
            }); //closes #main_content
        }
    });
    return false;
});

This ajax is only performed when the search form is submitted. After the php query returned the result ,I want to enable the user to sort the data of the json object. In order to do this, I need to store the object('data' in this case) to a global variable.It' because I have a function that will make use of this data to perform sorting!

只有在提交搜索表单时才会执行此ajax。在php查询返回结果后,我想让用户对json对象的数据进行排序。为了做到这一点,我需要将对象(在这种情况下为'data')存储到全局变量。因为我有一个函数将利用这些数据来执行排序!

2 个解决方案

#1


For that define a global variable

为此定义一个全局变量

var newData;
$("form").on("submit", function () {....
//Your code
...
success: function (data) {

  newData = data; //Assign the `data` to the global variable here
  ....

Now you can access newData outside AJAX.

现在您可以在AJAX外部访问newData。

#2


If you want to use within the script then keep it in the variable declare outside of function.

如果要在脚本中使用,请将其保存在函数外部的变量声明中。

Or if you want to use within the page anywhere use:

或者如果你想在任何地方使用页面内使用:

window.viewData.storeData=data;

#1


For that define a global variable

为此定义一个全局变量

var newData;
$("form").on("submit", function () {....
//Your code
...
success: function (data) {

  newData = data; //Assign the `data` to the global variable here
  ....

Now you can access newData outside AJAX.

现在您可以在AJAX外部访问newData。

#2


If you want to use within the script then keep it in the variable declare outside of function.

如果要在脚本中使用,请将其保存在函数外部的变量声明中。

Or if you want to use within the page anywhere use:

或者如果你想在任何地方使用页面内使用:

window.viewData.storeData=data;