1

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
 });
});
1
  • 1
    You have url on your first line and resturl in your AJAX call. Is this just a typo in your question? Commented Nov 10, 2010 at 9:11

4 Answers 4

2

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

Also see Richard's comment. Null is, indeed, not the same domain as your script is served from. And that matches the error better too. :) But if your site is not www.livetraffic.sg you will still need to proxy the data as I describe.
2

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.

Comments

0

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.

2 Comments

that's a big assumption the user has php server-side (if he has anything serverside at all).
Maybe but it's the simpler way IF USER HAS PHP SERVER-SIDE ^^
0

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 :)

Comments

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.