开发者

How to access a url using ajax from another url, same server just different domain

I have a server that is hosting 2 different domains, as far as I know I can't initiate a request from one domain on another using Javascript. So I am wondering how I can get around to do this?

What I'm trying to get done is on load get the json from the other url and then process it with javascript in the current url. So far I only have a button (a form with pre-filled input fields) that when you click it it takes me to the other url but I haven't figured out how can I make it so that it a) loads automatically and b) won't开发者_高级运维 take me to the other site just fetch the data and return it to me. My approach always ends up in ajax which I can't do. Any ideas?


You can have your JS add a <script> tag which loads the data (see http://en.wikipedia.org/wiki/JSONP).


The solution caould be JSONP (as suggested by rczajka) or in your case - since you have access to the other server, CORS - here is the explanation from MDN


The browser will request data from the domain's server by sending an Origin header with the value of the origin. It will only complete the connection if the server responds with an Access-Control-Allow-Origin header of either * or the exact URL of the requesting page. Web-server from another domain must send response header:

MDN: https://developer.mozilla.org/en/HTTP_access_control

MSDN: http://msdn.microsoft.com/en-us/library/dd573303%28v=vs.85%29.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜