失败的ajax请求,可能是由于一些叫做CORS的东西?

时间:2016-01-08 12:14:05

标签: jquery ajax cors

我有一个基本的ajax请求,它检索菜单并将其放在网页上。

这是代码

//display the menu
$.post(server+"quizz/phpScripts/retrieveMenu.php", {}, function(data){
    $("nav, #lowerMenu").html(data);
});

在原始文件中,' server'是本地主机',但当我托管该网站并尝试打开该页面时,我在控制台中收到此错误。

使用firefox

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://edwaze.com/quizz/phpScripts/retrieveMenu.php. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

使用Safari

XMLHttpRequest cannot load https://edwaze.com/quizz/phpScripts/retrieveMenu.php. Origin http://www.edwaze.com is not allowed by Access-Control-Allow-Origin.

使用Chrome

XMLHttpRequest cannot load https://edwaze.com/quizz/phpScripts/retrieveMenu.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://edwaze.com' is therefore not allowed access.

我已经阅读了相当多的内容,但我还没有找到解决方案。请帮忙。请轻松使用术语,我对ajax有点新鲜:)

感谢。

2 个答案:

答案 0 :(得分:2)

由于您没有提出任何跨域请求,因此最好使用root相对URL:

$.post("/quizz/phpScripts/retrieveMenu.php" 

前导/代表应用程序的根目录。

如果您在网址中添加了http/https协议,并且网址服务器上的网址托管在本地主机上,则会出现此问题。

答案 1 :(得分:0)

请浏览此网址。它可以帮助您解决查询。

CORS header 'Access-Control-Allow-Origin' missing

根据错误,您似乎需要设置Cross Origin,以便您可以访问其他域中的数据。