4

I am new to web and I need to get JSON object for a webpage that has data displayed this:

{
"expires": "2011-09-24T01:00:00",
"currencies": {
    "BZD": {
        "a": "2.02200",
        "b": "1.94826"
    },
    "YER": {
        "a": "220.050",
        "b": "212.950"
    }
}

I tried use jquery's $.getJSON to get the object but it didn't work.

<script> $.getJSON("http://m.somewebsite.com/data", { format: "json" }, function(data) { document.getElementById('test').innerHTML = data; }); </script>

I am wondering how to get this information correctly?

1

1 Answer 1

3

In order for this to work, you need to define jsonp, jsonp allows you to gain read access to another site's document, as the alternate version is barred.

$.getJSON("http://m.somewebsite.com/data?callback=?", { format: "json" }, function(data) { document.write(data); });
Sign up to request clarification or add additional context in comments.

3 Comments

I am unsure if $.getJSON is the correct command that I want to use. Because the function would work for the flicker api: "api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?" but not the link I had, even with the 'jsonp' at the end
To get a jsonp response using $.getJSON, you don't add a fourth argument. You need to add callback=? to the url. jsonp doesn't make an XHR request, but rather makes a script request that is not subject to the Same Origin Policy of XHR requests. The returned script is a function call, that passes the JavaScript object you're looking for as an argument. So if the server isn't set up to send a proper response for jsonp, it won't work.
Hmm, I see. Is there any alternative way of getting information from the website then?

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.