Reputation: 203
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
Upvotes: 0
Views: 47
Reputation: 7624
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]);
Upvotes: 0
Reputation: 15131
You need to parse first, the get then element you want:
//mind the closing parenthesis after data
alert(JSON.parse(data).SearchResults[0]);
Upvotes: 1