Matthew
Matthew

Reputation: 1655

Nested JSON response TypeError

I have a JSON response, one that I can't figure out. Here is my current response that I receive.

enter image description here

This is my ajax script:

<script>
 $(document).on('click', '#pullDetails', function() {
         $.ajax({
            type:'POST',
            url: '/carrier/claims/pullDetails',
            data: {
                num: $('input[name=proNumber]').val(),
                _token: $('input[name=_token]').val()},
            dataType: 'json',
            success: function(data) {
                if(data.details != undefined) {
                        console.log('success');
                        console.log(data.details.SearchResults[0].Shipment.Origin.Name); //result: ATLAS INTL
                        $('#carrierReturnData').html(data.details.SearchResults[0].Shipment.Origin.Name);
                    }else{
                        console.log('failed');
                        console.log(data);
                        console.log(data.details.SearchResults.SearchItem); 
                    }
            },
            error: function(data) {
                console.log('error');
                console.log(data);
            }
        });
    });
</script>

Now my question is how would I get the specific data from the line "SearchItem" (on the first line of the response json)?

At the moment I get the following: TypeError: data.details.SearchResults is undefined, but data.details is recognized as my console logs "success."

Image 1

Image 2

Image 3

Upvotes: 0

Views: 37

Answers (1)

Luciano Irigoyen
Luciano Irigoyen

Reputation: 26

Do a JSON.parse of details and then try to log SearchResults

var results = JSON.parse(data.details);
console.log(results.SearchResults.SearchItem);

Upvotes: 1

Related Questions