1

I have a url [https://www.inquicker.com/facility/americas-family-doctors.json] that is a JSON data url. How can I access the contents of this url, and write out the values. The format contains schedules as an array that inside it contains schedule_id, name, and available_times. I have tried various ways of getting the JSON file, but none have worked.

UPDATE: Well I have got it this far with this code, and it's laying out what looks like objects from the array. So I believe I got the cross site issue under control. I just need to figure out how to access the data now.

<!DOCTYPE html>
<html>
<head>
<title>JQuery (cross-domain) JSONP</title>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js">  </script>
<script>
$(document).ready(function(){
    $.getJSON('https://www.inquicker.com/facility/americas-family-doctors.json', 
        function(data){ 
        alert(data.facility);
        $.each(data.schedules, function(i, name){
            $('#names').append('<li>' + name.available_times[0] +     '</li>');
        });
    });
});
</script>
</head>
<body>
<ul id="names"></ul>
</body>
</html>

Any help, or suggestions will be greatly appreciated, Thanks.

4

3 回答 3

2

You cannot generally pass an Ajax request across domains. Normally a server will refuse any Ajax calls that don't come from the same source unless it is explicitly configured otherwise. I am guessing that you aren't calling from the same domain, given that you are using a fully-qualified URL. If you own the server, you will have to configure it to accept such calls from your other domain.

If this is not the case, launch the script in Firefox with Firebug running and look at the console output and tell me what error you get if any.


Once you manage to pass the JSON from your server back to the page, you will retrieve it in your JavaScript as a string. You then need to execute this function:

var jsonObject = JSON.parse(jsonString);

where jsonString is the string that you received from your server. jsonObject becomes an object representation of the JSON passed back to the answer that you can access using dot notation.

于 2012-12-11T07:17:57.013 回答
-1

Try something like :

alert(json.facility);

There is no title json object in the url you have mentioned.

于 2012-12-11T07:12:22.790 回答
-1

The JSON is already parsed when it comes to your function.

$.get('https://www.inquicker.com/facility/americas-family-doctors.json', function(result){ alert(result.facility); //Do whatever you want here

// result.schedules array is also ready });

于 2012-12-11T07:18:25.993 回答