当尝试将 json 值存储为来自返回的 ajax 的变量时 ...
$.ajax({
url:'example.php',
type:'POST',
dataType: 'json',
success:function(data){
var checkname = data.name; // working
var check1m = data.1m; // Here I get the SyntaxError
}
});
...它的列名以数字开头(例如 1m):
[{"name":"Peter","city":"London","1m":"not attending","2m":"attending"}]
我收到以下错误:
SyntaxError: identifier starts immediately after numeric literal
如何将值存储在变量 (check1m) 中?
最佳答案
您可以使用 offset
访问值。就像这样..
var check1m = data['1m'];
例子
var json = [{"name":"Peter","city":"London","1m":"not attending","2m":"attending"}] ;
console.log(json[0]['1m']);
关于javascript - Ajax/Json - 当列名是数字时如何将返回数据保存在变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42538053/