我们在虚拟机上安装了 Couchbase Server。我已经包含标签:
[cors]
methods: GET
origins: *
[httpd]
enable_cors = true
在我们的 local.ini 文件中,该文件位于:D:\Couchbase\etc\couchdb。在 PostMan 中,我可以执行: http:/xxxx:8093/query/service?statement= select member from edc
where member.general_info.member_id= '11111' 并查看返回的有效响应。但是当我们尝试在我们的 Web 应用程序中使用 JS 请求时,我们被阻止了。我们的 JS 看起来像:
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "xxxx:8093/query/service?statement=select member from `edc` where member.general_info.member_aces_id= '11111'", true);
xhReq.onload = function() {
console.log('Response from CORS request ' + xhReq.responseText);
};
xhReq.onerror = function() {
console.log('Woops, there was an error ');
};
xhReq.send();
我们在 Chrome 控制台中看到的响应是:
XMLHttpRequest 无法加载 http:/xxxx:8093/query/service?statement=select%20member%20from%20edc
%20where%20member.general_info.member_aces_id=%20%2711111%27。请求的资源上不存在“Access-Control-Allow-Origin” header 。因此,不允许访问源“http://localhost:8080”。
从 couchbase 文档中我可以在网上找到这应该可行。如果您希望我添加任何进一步的配置,请告诉我。感谢您的帮助!
最佳答案
这是因为 Chrome 网络安全不允许本地主机,请参阅 http://blog.chromium.org/2010/01/security-in-depth-new-security-features.html获取解释 - 在使用选项启动时尝试 Firefox 或覆盖 Chrome 网络安全:
--disable-web-security --user-data-dir
关于javascript - Couchbase 服务器未将 CORS header 返回到 XMLHttpRequest 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37620609/