简体   繁体   中英

AJAX Response to JSON Data

I have a JSON response, and the formatting is stumping me:

{
   "SearchResults": [
      {
         "SearchItem": "409885340",
         "Shipment": {
            "DRAvail": true,
            "ProNumber": "409885340",
            "PickupNumber": "24861628",
            "CustomerNumber": "688926",
            "BOLNumber": "100982156",
            "BOLReceived": true,
            "PODReceived": false,

And so on and so forth. Now when I return success in my AJAX (below):

success: function(data) {
        alert(data);
        alert(JSON.parse(data.SearchResults[0]));

The alert data returns:

{"SearchResults":[{"SearchItem":"409885340","Shipment":{"DRAvail":true,"ProNumber":"409885340","PickupNumber":"24861628","CustomerNumber":"688926","BOLNumber":"100982156","BOLReceived":true,"PODReceived":false,"PONumber":"04272018"...

But on the next line (where it is parsed), it returns this in the console:

TypeError: data.SearchResults is undefined

I know it's likely me being tired, but I'd appreciate any help in trying to get this to work...

Thanks

You need to parse first, the get then element you want:

//mind the closing parenthesis after data
alert(JSON.parse(data).SearchResults[0]);

IF your ajax response type is application/json you dont need to parse it

alert(data.SearchResults[0]);

will do

else

if return data is string you need to parse it and than get value

var parsedData = JSON.parse(data);
alert(parsedData.SearchResults[0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM