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.

Levi Botelho
  • 24,626
  • 5
  • 61
  • 96

3 Answers3

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.

Levi Botelho
  • 24,626
  • 5
  • 61
  • 96
  • The remote server needs to be configured to accept this. If you don't own the remote server, you can use your server side code as a sort of proxy, calling a local Ajax function which then calls the remote function from the server. See http://stackoverflow.com/questions/2558977/ajax-cross-domain-call for an idea of how to do this. – Levi Botelho Dec 11 '12 at 07:24
  • I think you have to use JSONP. Check this answer http://stackoverflow.com/a/3506306/513101 – Nick Dec 11 '12 at 07:27
  • @OptimusPrime - You don't have to necessarily, but it will also depend on his SS language. Basically you just need an HTTP call. In open source languages cUrl will do the trick just fine. .NET has its family of web requests etc. – Levi Botelho Dec 11 '12 at 07:31
  • You can return the JSON string directly from your method to your client app (with Ajax, since it is from your own domain) and parse it with `JSON.parse()`. At any rate, you have your data :) – Levi Botelho Dec 11 '12 at 07:50
  • You need to access the result of the parsing as an object. If your JSON object contains a property "name" : "joe", then when you invoke `JSON.parse(obj)` on the object you can then access the name from the resulting object by doing `resultingObject.name` – Levi Botelho Dec 11 '12 at 08:05
  • You need to brush up on the basics of JS + JSON parsing. `JSON.parse()` returns a JavaScript object. You simply need to work with the object that it returns. See https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects. (This is a segment of a tutorial, the entirety of which is very useful.) – Levi Botelho Dec 11 '12 at 08:29
  • Ok thanks for the info.. So do you recommend calling a proxy SS function rather than using JSONP? – Nick Dec 11 '12 at 09:11
  • I have been trying to put the string into an object like you were talking about but, I can't find anyways to do that. Been looking for some examples to make it work. I updated the code above to where I am, – Jason Wallace Dec 11 '12 at 09:40
  • The result of `JSON.parse()` is your object! – Levi Botelho Dec 11 '12 at 09:43
  • My updated code is writing out the first place of the JSON content for name.available_times[0] which is the first time in the array that I want, but it's still coming up undefined or object object. I suspect undefined is because there are no available times so the array is blank. Any help on this would be greatly appreciated, Thanks – Jason Wallace Dec 11 '12 at 09:45
  • Returning an object means that the array location contains another object. Returning undefined means the property does not exist. Re-read the other article as well as this stuff https://developer.mozilla.org/en/docs/JSON. You need to brush up on your basics so that you can understand what is going on here. If you need more help I'd be happy to oblige but you're going to have to post another question with code samples from your JS + JSON object because these comments will be moved to chat if we continue much longer. – Levi Botelho Dec 11 '12 at 09:56
  • Yes, I checked the JSON url, and indeed the undefined are just empty arrays, because they don't have any available times. I posted up a new Question http://stackoverflow.com/questions/13817976/json-data-and-manipulating-the-content – Jason Wallace Dec 11 '12 at 10:14
-1

Try something like :

alert(json.facility);

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

Vimalnath
  • 6,373
  • 2
  • 26
  • 47
-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 });

closure
  • 7,412
  • 1
  • 23
  • 23