0

I have this json result:

  [{
    "website": "http://www.rlbuht.nhs.uk",
    "sub_type": "Hospital",
    "sector": "NHS Sector",
    "postcode": "L3 5PS",
    "phone": "0151 706 2000",
    "partial_postcode": "L3",
    "parent_o_d_s_code": "RQ6",
    "parent_name": "Royal Liverpool and Broadgreen University Hospitals NHS Trust",
    "organisation_type": "Hospital",
    "organisation_status": "Visible",
    "organisation_name": "Royal Liverpool University Dental Hospital",
    "organisation_id": "41513",
    "organisation_code": "RQ614",
    "longitude": "-2.9669923782348633",
    "latitude": "53.408927917480469",
    "is_pims_managed": "True",
    "fax": "0151 706 5806",
    "email": "",
    "county": "Merseyside",
    "city": "Liverpool",
    "address3": "",
    "address2": "",
    "address1": "Pembroke Place"
}]

And here is my code to access the url and get the json which works fine:

$("#search_hospitals").on('click', function(p_oEvent_search_hospitals) {

     var api_search_hospitals, postcode, nhs_hospitals;
     p_oEvent_search_hospitals.preventDefault();
     postcode = $('#input_nhs_h').val();
     api_search_hospitals = 'https://data.gov.uk/data/api/service/health/hospitals/partial_postcode?partial_postcode=' + postcode;

     $.ajax(api_search_hospitals, {

         complete: function(p_OXHR, p_sStatus) {

             nhs_hospitals = $.parseJSON(p_OXHR.responseText);

             alert(nhs_hospitals[0].postcode);

         }

     });
 });

I'm just testing with alert(nhs_hospitals[0].postcode); to see if I can get the data but I always get TypeError: undefined is not an object (evaluating 'nhs_hospitals[0].postcode')...

Can someone please help me in saying what i'm doing wrong, why I'm getting "undefined"? Thank you

To make it easier I did:

complete: function(p_OXHR, p_sStatus) {

        nhs_hospitals = $.parseJSON(p_OXHR.responseText);
        hospital = nhs_hospitals.result[0];

        alert(hospital.postcode);

    }
2
  • What's is content of nhs_hospitals? Commented Sep 23, 2016 at 17:17
  • The problem is that the nhs_hospitals object has a result array inside it. If you reference that, you should get the result, as I've posted below Commented Sep 23, 2016 at 17:20

3 Answers 3

2

$('button').on('click', function(p_oEvent_search_hospitals) {

     var api_search_hospitals, postcode, nhs_hospitals;
     p_oEvent_search_hospitals.preventDefault();
     postcode = 'EC1A'
     api_search_hospitals = 'https://data.gov.uk/data/api/service/health/hospitals/partial_postcode?partial_postcode=' + postcode;

     $.ajax(api_search_hospitals, {

         complete: function(p_OXHR, p_sStatus) {

             nhs_hospitals = $.parseJSON(p_OXHR.responseText);
             console.log(nhs_hospitals);
             alert(nhs_hospitals.result[0].postcode);

         }

     });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>search</button>

This should work. The problem is that the nhs_hospitals object has a result array inside it. If you reference that, you will get your result.

Sign up to request clarification or add additional context in comments.

6 Comments

why should I use the "result" word before the [0]?
yeah can you cite any documentation explaining this?
Because that is the format of the response provided by your API. If you go to: https://data.gov.uk/data/api/service/health/hospitals/partial_postcode?partial_postcode=EC1A, you will see that the response has a success flag and a results array
@Aelliott1485 All of the documentation I have (and need) is that the API's response that can be seen here: https://data.gov.uk/data/api/service/health/hospitals/partia‌​l_postcode?partial_p‌​ostcode=EC1A As long as you don't have access to changing the API, you'll need to do a .result. I'm not sure if there's any documentation that the site provides, that might get you a different result
yeah I looked at the jQuery docs for jQuery.parseJSON() but it doesn't mention the result property...
|
1

Do a,

console.log(nhs_hospitals)

and see if it prints the array if not then that means p_OXHR.responseText does not contain anything. Are you sure the responseText is what is being sent from the server?

2 Comments

yep, I got {success: true, result: Array} in the console
OK if console.log(nhs_hospitals) prints {success: true, result: Array} then you should update the code to alert(nhs_hospitals.result[0].postcode); if not post the response
0

Try it:

 $.ajax(api_search_hospitals, {
                    success: function (data) {

                        nhs_hospitals = $.parseJSON(data);

                        alert(nhs_hospitals[0].postcode);

                    }

                });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.