Making an AJAX request to another server

后端 未结 4 702
小蘑菇
小蘑菇 2020-12-10 02:38

I have AJAX code where if you request an AJAX call to remote server the request fails:

function loadXMLDoc() {
  if (window.XMLHttpRequest) {
    // code for         


        
4条回答
  •  一个人的身影
    2020-12-10 03:11

    It looks like you have bumped into the same origin policy. You have to use a relative path instead of your absolute http://www.google.com path.

    As one possible workaround, you could set up a very simple reverse proxy (with mod_proxy if you are using Apache). This would allow you to use relative paths in your AJAX request, while the HTTP server would be acting as a proxy to any "remote" location.

    The fundamental configuration directive to set up a reverse proxy in mod_proxy is the ProxyPass. You would typically use it as follows:

    ProxyPass     /web-services/     http://third-party.com/web-services/
    

    In this case, the browser would be requesting /web-services/service.xml but the server would serve this by acting as a proxy to http://third-party.com/web-services/service.xml.

    Another common workaround would be to use JSONP.

提交回复
热议问题