What am I missing in the XMLHttpRequest?

后端 未结 3 358
梦如初夏
梦如初夏 2021-01-18 05:20

I\'m completely new to the javascript and ajax world but trying to learn.

Right now I\'m testing the XMLHttpRequest and I can\'t make work even the simplest example.

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-18 06:18

    Following from David's answer:

    You have to use a relative path to stay within the same origin policy. Otherwise most browsers will simply return an empty responseText and status == 0.

    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     /ajax/     http://google.com/
    

    In this case, the browser would be requesting /ajax/search?q=stack+overflow but the server would serve this by acting as a proxy to http://google.com/search?q=stack+overflow.

提交回复
热议问题