The code is very simple, I do not know why it deosnt work.
This is the link to the JSON file, http://webapp.armadealo.com/home.json
Here is the code using getJSON
$.getJSON("http://webapp.armadealo.com/home.json", function(data){
alert(data);
});
I just want the code to display the whole JSON content.
If you look in Chrome inspector, you probably see this error:
What this means is that the server doesn't want the client web page reading the file. The client isn't trusted. This is a basic security feature of XMLHttpRequest in order to prevent a site like mybank.evil.com from downloading data from mybank.com. It unfortunately makes testing from a local file challenging.
If you trust any site with your data or a select number of sites, you can configure your server script to send the
Access-Control-Allow-Origin
to allow certain sites through.See https://developer.mozilla.org/en/http_access_control for more details.
It should work.
Have you watched the request in firebug or another debug console, what happens and what the response is returned?
Please consider the same-origin-policy, so the script which makes this request, should also be loaded from webapp.armadealo.com. If not, you need a jsonp-request. Look at: http://api.jquery.com/jQuery.ajax/
From what I could tell is that your server doesn't support JSONP with their requests. For instance
This would say
SyntaxError: invalid label
. The return as you have it is not properly formatted JSONP. It has to be wrapped to work as JSONP because jQuery requires it.So what you're getting back is correct, but it's not what you need. What you need for JSONP calls would look like this:
... since what comes back currently is not valid JavaScript (by itself, and that's what it is), and that's how JSONP works, the response actually needs to be executable JavaScript.
You can get the same error by just plopping that code straight in your page in a
<script>
block.If you're just after the embedding piece of data, I recommend a plugin like jQuery-oembed, to do that. If you're after the data, you'll need something on your server to process the JSON, then get the data from your server after that.
For example
so let’s say we would like to make a cross-domain using jQuery. Here is how the jQuery
$.ajax
call should look like:Now on the server side (in the get_data.php file) we have to get the callback name and send the data in JSON format wrapped in the callback function. Something like this:
JSONP can only be used if and when the server properly embed the response in a JavaScript function call.
After so many months of search, I found the solution. Hence, I am answering my own question.
When JSON is not supported and when we are stuck with Same Origin Policy, we have to wrap around our JSON with a padding and make it a JSONP.
To do that, we have a life saving website http://anyorigin.com/
You can paste your URL and get the corresponding JQuery code something like this,
If you want to use your own code, then just use the URL from the code above, which is
This above URL will give you the same JSON data as JSONP and solves all the trouble.
I had used the following code, which on success calls displayAll function