Dayzza
Dayzza

Reputation: 1557

Help with getting Json Format data from external website

I am trying to get Json format data from this website .. http://www.livetraffic.sg/feeds/json

however when i use ajax.. i run into this particular error in my chrome console.

Error:XMLHttpRequest cannot load. Origin null is not allowed by Access-Control-Allow-Origin.

Is the external website preventing my from using information?

Thanks for your help!!!

Sample of my code:

url = "http://www.livetraffic.sg/home2/get_erp_gantry";   
$().ready(function(){ 
        $.get(resturl, function(data) {
        //do something here with data
 });
});

Upvotes: 1

Views: 2067

Answers (4)

Dayzza
Dayzza

Reputation: 1557

Thanks All! Manage to pull down the Json data from external website using a server side PHP script and then passing variables to my javascript :)

Upvotes: 0

MatTheCat
MatTheCat

Reputation: 18721

Simpler you can perform an ajax query to a local php page which contains

header("Content-type: application/json; charset=utf-8");
echo file_get_contents('http://www.livetraffic.sg/home2/get_erp_gantry');

You just must have allow_url_fopen true.

Upvotes: 0

Richard H
Richard H

Reputation: 39055

You cannot make cross-domain JSON requests. Your browser will not allow it. If the target domain allows JSONP requests http://en.wikipedia.org/wiki/JSONP#JSONP then you'll be able to use this work-around instead. Else you'll have to make the request server-side.

Upvotes: 2

cdhowie
cdhowie

Reputation: 168988

This is your browser enforcing the same-origin policy. You are not allowed to make requests to domains other than the domain your script was fetched from.

You will have to set up some server-side proxy on the same domain as the one your script is served from and have it supply the data. (You could also cache this data on the server if it would make sense.)

Upvotes: 2

Related Questions