我有以下返回 json 对象的 ajax 请求(我认为)。当我读取 console.log(response); 的结果时,您可以看到 cartcount 的值
我想将 var bill 设置为等于 cartcount 的值,在下面的示例中等于“1”。各种方法都试过了,但我怀疑是我理解错了json。
$.ajax({
url: app_config.ajax_cart_add,
type: 'POST',
data: data,
dataType: "json",
beforeSend: function() {
},
success: function(response) {
if (response.success === true) {
console.log(response);
var bill = $(response.cartcount);
console.log(bill);
App.success('Item was successful.');
}
if (response.success === false) {
App.error('There was a proble');
}
}
});
我的控制台日志为 console.log(response) 打印的内容;
{success: true, cartcount: "1"}
cartcount:"1"
success:true
proto:Object
a bunch of stuff in the proto object that seems irrelevant to my question.
最佳答案
不要将数据包装在 $()
中。它正在创建一个不必要的 jQuery 对象
改变
var bill = $(response.cartcount);
到
var bill = response.cartcount;
关于javascript - 从 ajax 响应中获取变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50430064/