JSON AJAX返回“Undefined”

时间:2017-11-30 06:33:27

标签: javascript jquery json ajax

我试图在我的ajax上获得一个简单的json,将值设置为我页面上的某些输入,但我总是在我的ajax警报上得到一个未定义的

这是我的JSON:

{"data":[{"Line_Number":10,"PO_Number":"P072592-1","Date":"9-07-17","Item_Number":"12789 - ","Size":"S","Description":"HDW- HINGE- PIANO- BLACK STAINLESS- .125 DIA.- HD SERIES","Delivery":"12-18-17","Quantity":800.0000,"Unit":"PCS","Unit_Price":0.8600,"Amount":"688"}]}

这是我的Ajax电话:

$.ajax({
    type: "GET",
    dataType: "json",
    url: '@Url.Action("GetOrder","Orders")/' + val,
    //data: { po_number: 'value' },
    success: function (data) {
        alert(data[0].Date)
    },
    error: function () {
         alert('Some error occurred!');
    }
});

但是我总是在警报上得到一个未定义的,我需要的是我的“日期”值 我做错了什么?

3 个答案:

答案 0 :(得分:6)

示例尝试此操作 - >

      var obj = {"data":[{"Line_Number":10,"PO_Number":"P072592-1","Date":"9-07-17","Item_Number":"12789 - ","Size":"S","Description":"HDW- HINGE- PIANO- BLACK STAINLESS- .125 DIA.- HD SERIES","Delivery":"12-18-17","Quantity":800.0000,"Unit":"PCS","Unit_Price":0.8600,"Amount":"688"}]};
       alert('Line_Number ='+obj.data[0].Line_Number+', PO_Number ='+obj.data[0].PO_Number);

答案 1 :(得分:1)

我看到你的数据是

{"data":[{"Line_Number":10,"PO_Number":"P072592-1","Date":"9-07-17","Item_Number":"12789 - ","Size":"S","Description":"HDW- HINGE- PIANO- BLACK STAINLESS- .125 DIA.- HD SERIES","Delivery":"12-18-17","Quantity":800.0000,"Unit":"PCS","Unit_Price":0.8600,"Amount":"688"}]}

所以你应该尝试一次

data.data[0].Date

答案 2 :(得分:1)

试试这个:

...
success: function (response) {
    alert(response.data[0].Date)
},
...

为什么呢? data是您的响应json中的一个键,因此您将收到一个包含它的对象(我将其简称为响应)。