AJAX json返回但不会找到值

时间:2015-04-24 15:42:58

标签: jquery ajax json vb.net

我目前在尝试从返回的JSON ajax响应中查找值时遇到问题。

ajax代码是:

$.ajax({
    url         : 'http://zzzzzzzz/Service.asmx',
    type        : 'POST',
    dataType    : 'text',
    contentType : "text/xml; charset=\"utf-8\"",
    data        : soapMessage,
    accepts     : 'json: application/json',
    converters  : {"text json": jQuery.parseJSON},
    crossDomain : true,
    error       : function() {
        console.log('An error has occurred');
    },
success     : function(data) {
            $(data).find('febRecordsResult').each(function () {
                var returnedJSON = JSON.parse($(this).text());
                var str = JSON.stringify(returnedJSON, null, 2)

                console.log(str.ReturnedData);

console.log输出如下所示:

undefined

Json结果如下所示( str ):

"{\r\n\"ReturnedData\": {\r\n\"howManyLevels\": \"5\",\r\n \"level-1-1-text\": \"test\", 
\r\n \"level-1-1-status\": \"test\", \r\n \"level-1-1-date\": \"4/15/2015\",
\r\n \"level-1-1-dd\": \"50%\", \r\n \"level-1-2-text\": \"test\", 
\r\n \"level-1-2-status\": \"test\", \r\n \"level-1-2-date\": \"4/15/2015\",
\r\n \"level-1-2-dd\": \"25%\", \r\n \"level-1-3-text\": \"test\",    
etc etc....\"\r\n}\r\n}"

那么为什么我无法选出 ReturnedData

console.log(data)显示:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><febRecordsResponse xmlns="http://empDetails.bcbst.com/"><febRecordsResult>"{\r\n\"ReturnedData\": {\r\n\"howManyLevels\": \"5\",\r\n \"level-1-1-text\": \"test\", \r\n \"level-1-1-status\": \"test\", \r\n \"level-1-1-date\": \"4/15/2015\",\r\n \"level-1-1-dd\": \"50%\", \r\n \"level-1-2-text\": \"test\", \r\n \"level-1-2-status\": \"test\", \r\n \"level-1-2-date\": \"4/15/2015\", \r\n \"level-1-2-dd\": \"25%\", \r\n \"level-1-3-text\": \"test\", \r\n \"level-1-3-status\": \"test\", \r\n \"level-1-3-date\": \"4/23/2015\", \r\n \"level-1-3-dd\": \"50%\", \r\n \"level-2-1-text\": \"test\", \r\n \"level-2-1-status\": \"test\", \r\n \"level-2-1-date\": \"4/23/2015\",\r\n \"level-2-1-dd\": \"N/A\", \r\n \"level-2-2-text\": \"test\", \r\n \"level-2-2-status\": \"test\", \r\n \"level-2-2-date\": \"4/8/2015\", \r\n \"level-2-2-dd\": \"N/A\", \r\n \"level-2-3-text\": \"test\", \r\n \"level-2-3-status\": \"test\", \r\n \"level-2-3-date\": \"3/31/2015\", \r\n \"level-2-3-dd\": \"N/A\", \r\n \"level-3-1-text\": \"test\", \r\n \"level-3-1-status\": \"test\", \r\n \"level-3-1-date\": \"4/10/2015\",\r\n \"level-3-1-dd\": \"N/A\", \r\n \"level-3-2-text\": \"test\", \r\n \"level-3-2-status\": \"test\", \r\n \"level-3-2-date\": \"5/1/2015\", \r\n \"level-3-2-dd\": \"50%\", \r\n \"level-3-3-text\": \"test\", \r\n \"level-3-3-status\": \"test\", \r\n \"level-3-3-date\": \"4/18/2015\", \r\n \"level-3-3-dd\": \"N/A\", \r\n \"level-4-1-text\": \"test\", \r\n \"level-4-1-status\": \"test\", \r\n \"level-4-1-date\": \"3/30/2015\",\r\n \"level-4-1-dd\": \"50%\", \r\n \"level-4-2-text\": \"test\", \r\n \"level-4-2-status\": \"test\", \r\n \"level-4-2-date\": \"4/24/2015\", \r\n \"level-4-2-dd\": \"50%\", \r\n \"level-4-3-text\": \"test\", \r\n \"level-4-3-status\": \"test\", \r\n \"level-4-3-date\": \"5/9/2015\", \r\n \"level-4-3-dd\": \"100%\", \r\n \"level-5-1-text\": \"test\", \r\n \"level-5-1-status\": \"test\", \r\n \"level-5-1-date\": \"3/30/2015\",\r\n \"level-5-1-dd\": \"25%\", \r\n \"level-5-2-text\": \"test\", \r\n \"level-5-2-status\": \"test\", \r\n \"level-5-2-date\": \"3/30/2015\", \r\n \"level-5-2-dd\": \"100%\", \r\n \"level-5-3-text\": \"test\", \r\n \"level-5-3-status\": \"test\", \r\n \"level-5-3-date\": \"4/11/2015\", \r\n \"level-5-3-dd\": \"75%\"\r\n}\r\n}"</febRecordsResult></febRecordsResponse></soap:Body></soap:Envelope>

之后回来我用:

var returnedJSON = JSON.parse($(this).text());

这使它看起来像这样:

{
"ReturnedData": {
"howManyLevels": "5",
 "level-1-1-text": "test", 
 "level-1-1-status": "test", 
 "level-1-1-date": "4/15/2015",
 "level-1-1-dd": "50%", 
 "level-1-2-text": "test", 
 "level-1-2-status": "test", 
 "level-1-2-date": "4/15/2015", 
 "level-1-2-dd": "25%", 
 "level-1-3-text": "test", 
 "level-1-3-status": "test", 
 "level-1-3-date": "4/23/2015", 
 "level-1-3-dd": "50%", 
 "level-2-1-text": "test", 
 "level-2-1-status": "test", 
 "level-2-1-date": "4/23/2015",
 "level-2-1-dd": "N/A", 
 "level-2-2-text": "test", 
 "level-2-2-status": "test", 
 "level-2-2-date": "4/8/2015", 
 "level-2-2-dd": "N/A", 
 "level-2-3-text": "test", 
 "level-2-3-status": "test", 
 "level-2-3-date": "3/31/2015", 
 "level-2-3-dd": "N/A", 
 "level-3-1-text": "test", 
 "level-3-1-status": "test", 
 "level-3-1-date": "4/10/2015",
 "level-3-1-dd": "N/A", 
 "level-3-2-text": "test", 
 "level-3-2-status": "test", 
 "level-3-2-date": "5/1/2015", 
 "level-3-2-dd": "50%", 
 "level-3-3-text": "test", 
 "level-3-3-status": "test", 
 "level-3-3-date": "4/18/2015", 
 "level-3-3-dd": "N/A", 
 "level-4-1-text": "test", 
 "level-4-1-status": "test", 
 "level-4-1-date": "3/30/2015",
 "level-4-1-dd": "50%", 
 "level-4-2-text": "test", 
 "level-4-2-status": "test", 
 "level-4-2-date": "4/24/2015", 
 "level-4-2-dd": "50%", 
 "level-4-3-text": "test", 
 "level-4-3-status": "test", 
 "level-4-3-date": "5/9/2015", 
 "level-4-3-dd": "100%", 
 "level-5-1-text": "test", 
 "level-5-1-status": "test", 
 "level-5-1-date": "3/30/2015",
 "level-5-1-dd": "25%", 
 "level-5-2-text": "test", 
 "level-5-2-status": "test", 
 "level-5-2-date": "3/30/2015", 
 "level-5-2-dd": "100%", 
 "level-5-3-text": "test", 
 "level-5-3-status": "test", 
 "level-5-3-date": "4/11/2015", 
 "level-5-3-dd": "75%"
}
}

但是当我选择 ReturnedData 时,仍然会说 undefined

console.log(returnedJSON.ReturnedData);

console.log(returnedJSON['ReturnedData']);

3 个答案:

答案 0 :(得分:0)

您的返回值是data.d

答案 1 :(得分:0)

不应该是dataType = json吗?如果不是console.log(str)并查看该对象并查看它返回的值。

答案 2 :(得分:0)

我需要执行以下操作才能找到值:

$(data).find('febRecordsResult').each(function (dataa) {
    var returnedJSON = JSON.parse($(this).text());

    returnedJSON = returnedJSON.ReturnedData[0];
    $('#howManyRecs').append(returnedJSON.howManyLevels);
    $('#level1text').append(returnedJSON['level-1-1-text']);
    $('#level1status').append(returnedJSON['level-1-1-status']);
    $('#level1date').append(returnedJSON['level-1-1-date']);
    $('#level1dd').append(returnedJSON['level-1-1-dd']);
});