1

I need to read a JSON file from URL and display. I have read a lot of posts but still couldn't fix the problem.

url : http://webapp.armadealo.com/home.json

I face this error : XMLHttpRequest cannot load

The code is below

$.getJSON("http://webapp.armadealo.com/home.json", function(data){
alert(data);
});

I have tried adding to the url

&callback=?

and making it a jsonp, still no luck. I have also used

<meta http-equiv="Access-Control-Allow-Origin" content="*" />

still no luck.

Is there anything that we need to do at the server side? People who have faced such an error and have found a solution, Help me out please! Thanks a lot!

LINGS
  • 3,560
  • 5
  • 34
  • 47
  • What is the domain where your script lives? And are you in control of the webapp server? If so, could you show us the relevant serverside code? – Bergi Jul 16 '12 at 20:41
  • 1
    What does that meta-tag do there? You want to serve JSON, you will need to send it as a HTTP *header*. – Bergi Jul 16 '12 at 20:43

1 Answers1

4

You cannot make cross-domain AJAX requests like that due to security reasons. So if you want to load content from another domain, you will have to use a workaround: JSONP (more info, example)

Use the following code for the AJAX request:

$.ajax({
    url: 'http://webapp.armadealo.com/home.json',
    type: 'GET',
    jsonpCallback: 'myCallback',
    dataType: "jsonp",
    success: function(data) {
        console.log(data);
    }
});

In order for this to work, you will have to wrap the JSON data in parentheses and add the callback name at the beginning:

myCallback({ ... JSON ... })


EDIT: Just noticed you already tried to use JSONP... Well, at least the above code works for me, perhaps you want to give it a try. ;)

Aletheios
  • 3,960
  • 2
  • 33
  • 46
  • Thank you for your reply. But for the above code were you able to get the data? lets say if you were to just `alert(data)` do you get the output? I get unexpected token error at the javascript console, chrome browser. – LINGS Jul 18 '12 at 16:18
  • 1
    Did you add the name of the callback function to your JSON data? It doesn't work without (so you definitely must have access to the JSON file or the generation routine)... In order to test my code above, I downloaded your JSON file, added the callback and uploaded it to my own webserver. – Aletheios Jul 18 '12 at 19:13
  • Ok great, thank you! I will have to work on the server end then. I was hoping that I could find an alternative without altering the server. – LINGS Jul 20 '12 at 03:33