简体   繁体   English

Couchbase Server未将CORS标头返回给XMLHttpRequest对象

[英]Couchbase Server not returning CORS Header to XMLHttpRequest object

We have Couchbase Server installed on a VM. 我们在VM上安装了Couchbase Server。 I have include the tags: 我已经包含了标签:

 [cors]
 methods: GET
 origins: *

 [httpd]
 enable_cors = true

in our local.ini file located at: D:\\Couchbase\\etc\\couchdb. 在我们的local.ini文件中,位于:D:\\ Couchbase \\ etc \\ couchdb。 In PostMan I am able to execute: http:/xxxx:8093/query/service?statement= select member from edc where member.general_info.member_id= '11111' and see a valid response returned. 在PostMan中我能够执行:http:/ xxxx:8093 / query / service?statement =从edc中选择成员,其中member.general_info.member_id ='11111'并看到返回的有效响应。 But when trying to request with JS in our web application we are blocked. 但是当我们在Web应用程序中尝试使用JS时,我们被阻止了。 Our JS looks like: 我们的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();

The response we see in Chrome console is: 我们在Chrome控制台中看到的响应是:

XMLHttpRequest cannot load http:/xxxx:8093/query/service?statement=select%20member%20from%20 edc %20where%20member.general_info.member_aces_id=%20%2711111%27. XMLHttpRequest无法加载http:/ xxxx:8093 / query / service?statement = select%20member%20from%20 edc %20 where%20member.general_info.member_aces_id =%20%2711111%27。 No 'Access-Control-Allow-Origin' header is present on the requested resource. 请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http:/localhost:8080' is therefore not allowed access. 原因'http:/ localhost:8080'因此不允许访问。

From the couchbase documentation I could find online this should work. 从我可以在网上找到的沙发基础文档,这应该工作。 Please let me know if you want me to include any further configurations. 如果您希望我包含任何进一步的配置,请告诉我。 Thanks for the help! 谢谢您的帮助!

这是因为Chrome网络安全不允许使用localhost,请参阅http://blog.chromium.org/2010/01/security-in-depth-new-security-features.html以获取解释 - 尝试使用Firefox或覆盖Chrome网站从选项开始时的安全性:

--disable-web-security --user-data-dir

While trying to use XMLHttpRequest you cannot set responseType while in asynchronous mode. 尝试使用XMLHttpRequest时,在异步模式下无法设置responseType。 To avoid this i decided to use ajax like: 为了避免这种情况,我决定使用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));
    }
  });

I also have switched to Firefox from Chrome as chrome does not allow this even with CORS extension. 我也从Chrome切换到Firefox,因为即使使用CORS扩展,chrome也不允许这样做。 I am using cors everywhere with Firefox as well. 我也在使用Firefox随处可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM