Help with getting Json Format data from external website

后端 未结 4 1429
面向向阳花
面向向阳花 2021-01-23 13:59

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 con

相关标签:
4条回答
  • 2021-01-23 14:29

    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.

    0 讨论(0)
  • 2021-01-23 14:31

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

    0 讨论(0)
  • 2021-01-23 14:41

    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.

    0 讨论(0)
  • 2021-01-23 14:46

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

    0 讨论(0)
提交回复
热议问题