Couchbase 服务器未将 CORS Header 返回到 XMLHttpRequest object
Couchbase Server not returning CORS Header to XMLHttpRequest object
我们在虚拟机上安装了 Couchbase 服务器。我已经包含了标签:
[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
尝试使用 XMLHttpRequest 时,您无法在异步模式下设置 responseType。为了避免这种情况,我决定使用 ajax,例如:
$.ajax({
url: "http://xxxx:8093/query/service?statement=select member from `edc` where member.general_info.member_aces_id='11111'",
responseType: 'json',
type: 'GET',
success: function(data) {
console.log("data: " + JSON.stringify(data));
},
error: function(xhr, status, err) {
console.log("error: " + err + " xhr: " + JSON.stringify(xhr));
}
});
我也已从 Chrome 切换到 Firefox,因为 chrome 即使使用 CORS 扩展也不允许这样做。我也在 Firefox 中到处使用 cors。
我们在虚拟机上安装了 Couchbase 服务器。我已经包含了标签:
[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
尝试使用 XMLHttpRequest 时,您无法在异步模式下设置 responseType。为了避免这种情况,我决定使用 ajax,例如:
$.ajax({
url: "http://xxxx:8093/query/service?statement=select member from `edc` where member.general_info.member_aces_id='11111'",
responseType: 'json',
type: 'GET',
success: function(data) {
console.log("data: " + JSON.stringify(data));
},
error: function(xhr, status, err) {
console.log("error: " + err + " xhr: " + JSON.stringify(xhr));
}
});
我也已从 Chrome 切换到 Firefox,因为 chrome 即使使用 CORS 扩展也不允许这样做。我也在 Firefox 中到处使用 cors。