[英]Get variable value from ajax response
I have the following ajax request which returns a json object (I think). 我有以下ajax请求,它返回一个json对象(我认为)。 You can see the value of cartcount when I read the results of console.log(response);
当我读取console.log(response);的结果时,您可以看到cartcount的值。
I want to set var bill equal to the value of cartcount, which equals "1" in the below example. 我想将var bill设置为等于cartcount的值,在下面的示例中等于“ 1”。 I have tried a variety of ways, but I suspect I am misunderstanding json.
我已经尝试了多种方法,但是我怀疑我误解了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');
}
}
});
What my console log prints for console.log(response); 我的控制台日志为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.
Don't wrap the data in $()
. 不要将数据包装在
$()
。 It is creating a needless jQuery object 它正在创建一个不必要的jQuery对象
Change 更改
var bill = $(response.cartcount);
To 至
var bill = response.cartcount;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.