Sending data to an external file via Ajax

2019-09-03 09:37发布

When I use this code, it works:

ajax.open("post","a.php",true);

but when I try to send data to a external file like:

ajax.open("post","http://www.example.com/a.php",true);

it doesn't work.

Are there any solution?

2条回答
甜甜的少女心
2楼-- · 2019-09-03 10:07

The URL of the file that must be opened - the location of the server side script. This can be a absolute URL like(http://www.foo.com/bar.php) or a relative one(/bar.php). A note of caution - this URL should be in the same domain as the script is. You cannot call a script in google.com from a script that is running in yahoo.com. This is a security measure implemented in most browsers to prevent XSS.

Regards, Cyril

查看更多
祖国的老花朵
3楼-- · 2019-09-03 10:19

On which domain is your script executed? Is it www.site.com or some other? The reason your code might not work is because for security reasons you are not allowed to send AJAX request to other domains.

Edit: One workaround would be to implement a web service on mysite.com, send AJAX request to it. The service should then proxy the original request to othersite.com (server-side) and subsequently return the response to the script being executed on mysite.com.

查看更多
登录 后发表回答