我有一个在php中创建的javascript变量
echo "var".$locallist."=".create_js_variable($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函数相同)
if($what == 'db2list') {
$db2_list_all = get_db2_database_list();
echo create_js_variable($locallist,$db2_list_all,'db2');
}
Console.log 报告
- 更新之前变量是一个对象
- 更新后它是一个文本
我该如何解决这个问题?那么我的其他 javascript 代码又可以工作了吗?
最佳答案
好吧,看看你的 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 对象,就像您已经拥有的那样。
关于javascript - 如何通过ajax更新js对象类型变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7200766/