0

Possible Duplicate:
XMLHttpRequest cannot load an URL with jQuery

I am using the jquery get function to get the data with in the link

http://stormtrack.srcc.lsu.edu/php/getStormYearAsJson.php

but when I run it i am getting an error saying "xmlhttprequest cannot load". Can any one suggest me how I could parse this file to get the data.

$(document).ready(function() {
    $.getJSON("http://stormtrack.srcc.lsu.edu/php/getStormYearAsJson.php", function(data){
        alert("Data Loaded: " + data);
    });
});
Community
  • 1
  • 1
Joel James
  • 3,870
  • 9
  • 32
  • 47

5 Answers5

0

You can't access the url unless your script runs on the domain http://stormtrack.srcc.lsu.edu due to the same origin policy.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
0

You're falling foul of the Same Origin Policy. Browsers don't allow cross-domain requests.

If the API you're trying to use supports it, try using JSONP. You can use jQuery's nice $.getJSON wrapper function to do this.

Alex
  • 9,313
  • 1
  • 39
  • 44
0

AJAX is restricted by the Same Origin Policy, which says you can only get content from the same hostname/domain as your script.

jQuery lets you work around this using a thing called JSONP. This is where the url you call sends a function name and the php script wraps the reply in a js function call.

Change your javascript request to:

http://stormtrack.srcc.lsu.edu/php/getStormYearAsJson.php?callback=myFunc

Then wrap your php output in

function myFunc {
    ...
}
Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
0

You can't use XHR to access data on another domain due to the same origin policy. Either use JSONP or create a proxy in PHP on the same domain that will fetch that data for you and return it as is.

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
0

Reason:

AJAX calls can only be done within the same Domain. This is why you have this error.

Workaround:

If you want to get data from an external website, you need to create your own server side page that will do the query to the other website. From there, you can create a AJAX call to this new page that will call the other server.

Community
  • 1
  • 1
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341