簡體   English   中英

從ajax響應獲取變量值

[英]Get variable value from ajax response

我有以下ajax請求,它返回一個json對象(我​​認為)。 當我讀取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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM