Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过ajax更新js对象类型变量?_Javascript_Jquery - Fatal编程技术网

Javascript 如何通过ajax更新js对象类型变量?

Javascript 如何通过ajax更新js对象类型变量?,javascript,jquery,Javascript,Jquery,我有一个javascript变量,它是在php中创建的 echo“var”。$locallist.=>。创建_js_变量($locallist,$db2_list_all,'db2') 它在html页面源代码中看起来像 var locallist={ 'CARINYA':[['2011-08-24-09-22 - w','20110824092216w'],['2011-08-18-13-15','20110818131546']], 'COVERNAN':[['2011-03-02-1

我有一个javascript变量,它是在php中创建的

echo“var”。$locallist.=>。创建_js_变量($locallist,$db2_list_all,'db2')

它在html页面源代码中看起来像

var locallist={
  'CARINYA':[['2011-08-24-09-22 - w','20110824092216w'],['2011-08-18-13-15','20110818131546']],
  'COVERNAN':[['2011-03-02-12-28','20110302122831']],
  'DAVID':[['2010-12-22-19-43','20101222194348'],['2010-12-08-14-10','20101208141035']]};
现在我想通过ajax

    jQuery.ajax({
        type: 'get',
        dataType: 'text',
        url: url,
        data: {
            what: 'db2list', 
            t: Math.random() 
        },
        success: function(data, textStatus){
            locallist = data;                   
            console.log(locallist);
        }
    });
ajax调用了这段php代码(注意,调用的是同一个php函数)

Console.log报告

  • 更新前,变量是一个对象
  • 更新后,它是一个文本

我怎样才能解决这个问题?那么我的另一个javascript代码又能工作了吗?

您的AJAX请求有一个类型
数据类型:'text',

将其更改为JSON:)

您的AJAX请求有一个类型
数据类型:'text',

将其更改为JSON:)

好吧,看看您的ajax调用:

jQuery.ajax({
    type: 'get',
    dataType: 'text',
    url: url,
    data: {
        what: 'db2list', 
        t: Math.random() 
    },
    success: function(data, textStatus){
        locallist = data;                   
        console.log(locallist);
    }
});

注意,您将数据类型设置为文本?它将把数据导入并将其作为文本处理。尝试将数据类型更改为“json”。这将把引入的数据转换为常规的“ol”对象,就像您已经拥有的一样

好吧,看看你的ajax调用:

jQuery.ajax({
    type: 'get',
    dataType: 'text',
    url: url,
    data: {
        what: 'db2list', 
        t: Math.random() 
    },
    success: function(data, textStatus){
        locallist = data;                   
        console.log(locallist);
    }
});

注意,您将数据类型设置为文本?它将把数据导入并将其作为文本处理。尝试将数据类型更改为“json”。这将把引入的数据转换为常规的“ol”对象,就像您已经拥有的一样

使用
数据类型:“json”

jQuery.ajax({
    type: 'get',
    dataType: 'json',
    url: url,
    data: {
        what: 'db2list', 
        t: Math.random() 
    },
    success: function(data, textStatus){
        locallist = data;                   
        console.log(locallist);
    }
});

使用
数据类型:“json”

jQuery.ajax({
    type: 'get',
    dataType: 'json',
    url: url,
    data: {
        what: 'db2list', 
        t: Math.random() 
    },
    success: function(data, textStatus){
        locallist = data;                   
        console.log(locallist);
    }
});

我必须在服务器端更改任何内容吗?我可以在javascript中修复它吗?我必须在服务器端更改任何内容吗?我可以在javascript中修复它吗?我必须在服务器端更改任何内容吗?我可以在javascript中修复它吗?您不需要在服务器端更改任何内容。如果它不起作用,一个相对简单的修复方法是在您吐出任何内容之前将其放入代码中:header(“Content-type:application/json”);或者只是标题(“内容类型:文本/普通”);我喜欢这个,因为如果我把它加载到我的浏览器上,我就可以看到这个页面了。它现在可以正常工作了,但在我看来,其他js函数没有接收到更新。我在该功能中单击更改选择选项。我可以看到ajax返回了正确的数据,但变量的内容是相同的。因此,将类型更改为json后,不会执行success函数。我是否必须更改服务器端的任何内容?我可以在javascript中修复它吗?您不需要在服务器端更改任何内容。如果它不起作用,一个相对简单的修复方法是在您吐出任何内容之前将其放入代码中:header(“Content-type:application/json”);或者只是标题(“内容类型:文本/普通”);我喜欢这个,因为如果我把它加载到我的浏览器上,我就可以看到这个页面了。它现在可以正常工作了,但在我看来,其他js函数没有接收到更新。我在该功能中单击更改选择选项。我可以看到ajax返回了正确的数据,但变量的内容是相同的。因此,将类型更改为json后,不会执行成功函数。请确保在json中使用
,而不是
谢谢@Stephen确保在json中使用
谢谢@Stephen