JavaScript 的 Access-Control-Allow-Origin 错误
我正在尝试使用简单的 Javascript 书签将一些内容加载到我浏览的网站上。因为我希望让任何人都可以使用它,而不仅仅是我,所以我发出一个具有与书签相关联的唯一标识符的 get 请求,如下所示:
$.get("http://www.mysite.com/dothis.php?id=1234", {}, function(data) {
$("body").append(data);
});
问题是我得到了
XMLHttpRequest cannot load http://www.mysite.com/dothis.php?id=1234. Origin http://www.google.com is not allowed by Access-Control-Allow-Origin.
问题似乎我可以识别该请求(例如,dothis.php 可以保存我向数据库发出 get 请求的事实,但它无法将任何数据返回给请求者。这是有道理的,但是有什么办法可以解决吗? iFrame 不起作用,因为我最终也需要对返回的数据进行操作。
谢谢!
I'm trying to load a bit of content onto sites that I browse to using a simple Javascript bookmarklet. Since I'm hoping to make this usable by anyone and not just me, I make a get request that has a unique identifier associated with the bookmarklet, like so:
$.get("http://www.mysite.com/dothis.php?id=1234", {}, function(data) {
$("body").append(data);
});
The trouble is that I get
XMLHttpRequest cannot load http://www.mysite.com/dothis.php?id=1234. Origin http://www.google.com is not allowed by Access-Control-Allow-Origin.
The issue seems that I can recognize the request (dothis.php can save the fact that I made the get request to a database, for example) but it cannot return any data back to the requester. It makes sense, but is there some way around it? iFrames won't work because I'll need to act upon that returned data, too, eventually.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的 PHP 脚本中,您可以允许来自每个网站的 Access-Control-Allow-Origin。只需将其
放在 php 文件的最开头即可。我还没有测试过这个,但它应该可以工作。
In your PHP script you can allow Access-Control-Allow-Origin from every website. Just put
to the very beginning of your php file. I haven't tested this, but it ought to work.