How to perform cross-site ajax request?

前端 未结 3 958
半阙折子戏
半阙折子戏 2021-01-22 14:42

Browsers don\'t allow cross-site AJAX calls (it\'s a security restriction). Is there any possible solution ?

EDIT

I control only the caller website

3条回答
  •  無奈伤痛
    2021-01-22 14:52

    If you control both parties then there a lot of options. Such as JSONP, or modifying header responses of the remote website. Unfortunately, JSONP only works if the remote website supports it. You can't force a JSONP call to a website that doesn't already support it.

    However, as you have said, you only control the source website. You cannot hack the browser around this restriction for obvious reasons. You do have a third option which is creating a back-end proxy. You can use Apache and mod_rewrite to create a proxy. Here is on how to do this or this link which is more detailed.

    For example

    ProxyPass /api/gtalkbots http://gtalkbots.com/reverse-proxy-data.php  
    ProxyPassReverse /api/gtalkbots http://gtalkbots.com/reverse-proxy-data.php  
    

    Creates a proxy at /api/gtalkbots which will returns the repose from gtalkbots.com

提交回复
热议问题