Using Ajax return data to fill a span tag

时间:2018-01-15 18:12:34

标签: javascript jquery ajax razor

I have this ajax call that is called when data is successfully update:

function success(data) {
var userId = $('#ID').val();
$.ajax({
    url: "/Orders/DraftOrderDetailsLineItems",
    type: 'GET',
    async: false,
    cache: false,
    datatype: 'json',
    data: { "customerId": userId },
    success: function (data) {
        if (data != "") {
                $('#draftOrderItems').html(data);
                $('#lineItemTable').show();
                $('#draftOrderShipping').show();
                $('#totalDraftSalePrice').html(data.SubtotalBasePrice);

            //TODO: if this is the last item, hide the lineitems
        }
        console.log("Call line item partial" + data);

    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.error("[Error in Ajax Request, Get Line Items] Code:" + jqXHR.status + " Error:" + errorThrown + " \nText Status:" + jqXHR.responseText);
    }
});

}

The problem I am having is I am trying to get the HTML for the totalDraftSalePrice to update with the new value after the update (in the data). The data field is called SubTotalBasePrice. I can see it in my json visualizer and search for it in the visualizer, I just can't seem to get the code to update the HTML tag. What am I doing wrong here?

Thanks.

2 个答案:

答案 0 :(得分:1)

响应数据类型字段为dataType而非datatype
这意味着data是一个字符串而不是一个对象,因此data.SubtotalBasePrice未定义,因为字符串没有成员SubtotalBasePrice

所以你的要求应该是

dataType: 'json',

答案 1 :(得分:0)

Are you able to console the value of "data.SubtotalBasePrice"

if not try to console.log(data.SubtotalBasePrice); and
if it is in array try data[0].SubtotalBasePrice ,may be this can work or data['SubtotalBasePrice']