I have a php file on a server, which just prints random numbers.
I want to get these numbers by using XMLHttpRequest
from another domain.
How 开发者_如何学JAVAcan I do this?
You can't.
XHR is subject to the same origin policy. There is ongoing work to design and implement systems to allow cross-domain XHR, but the current state of those (the lack of browser support in particular) make it impractical for any real project.
The usual method to work around this is to implement the system using JSON-P instead of XHR.
You can use Jquery.ajax
http://api.jquery.com/jQuery.ajax/
You can implement CORS
(Cross-Origin Resource Sharing) on your server.
The current versions of Google Chrome, Firefox and Safari support it now.
The next versions of Opera and Internet Explorer also support it but have not been released yet as of the beginning of March 2012.
精彩评论