【发布时间】:2018-05-29 20:11:21
【问题描述】:
我正在使用 cloudant,CORS 已启用,除@987654327@ 之外的所有查询都正常工作。
我的 find 查询使用以下代码在本地 CouchDB 上运行良好:
var urlPrefix = "http://localhost:5984/etablissements";
var query = {
"selector": {
"cp": 24000
}
};
$.ajax({
type: "POST",
url: urlPrefix + '/_find',
contentType: "application/json",
data:JSON.stringify(query),
success: function(data) {
console.log(data.docs);
// do whatever with data.docs there
}
});
现在,我想在线进行 CORS jQuery AJAX 调用,并针对我的 IBM Cloudant 数据库执行与第一个相同的查询,但它不起作用,虽然它是相同的查询,并且相同的复制数据库。
在 Cloudant 上启用了 CORS。
urlPrefix = "https://USERNAME:PASSWORD@HOST.cloudant.com/etablissements";
var query = {
"selector": {
"cp": 24000
}
};
$.ajax({
type: "POST",
url: urlPrefix + '/_find',
contentType: "application/json",
data:JSON.stringify(query),
dataType: 'json',
crossDomain: true,
xhrFields: {
'withCredentials': true // tell the client to send the cookies if any for the requested domain
},
success: function(data) {
console.log(data.docs);
}
},
error:function(data){
console.log(data);
}
});
我没有收到来自 Cloudant 的任何错误消息。我刚刚从 jQuery 得到这个错误消息:
{…}
abort: function abort()
always: function always()
catch: function catch()
done: function add()
fail: function add()
getAllResponseHeaders: function getAllResponseHeaders()
getResponseHeader: function getResponseHeader()
overrideMimeType: function overrideMimeType()
pipe: function pipe()
progress: function add()
promise: function promise()
readyState: 0
responseJSON: undefined
setRequestHeader: function setRequestHeader()
state: function state()
status: 0
statusCode: function statusCode()
statusText: "error"
then: function then()
__proto__: Object { … }
此文档中的其他查询(例如“全部”)当前正在运行:
http://bradley-holt.com/2011/07/couchdb-jquery-plugin-reference/
编辑: Glynn,最新的 firefox 版本和您复制粘贴的代码仍然出现相同的错误,没有完成 ajax 调用,而是通过 jquery 错误过程:
编辑 2: 我已经更改了 Glynn 的一些代码,并且它在 chrome 中运行,但我必须在来自 Chrome 的弹出窗口中提供凭据。请注意,我已经从 urlPrefix 变量中删除了登录密码,并且 jquery 中提供的用户名和密码不起作用。所以这还不是很好的解决方案。 Firefox 仍然失败并出现上面显示的错误:
<html>
<body>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script>
var urlPrefix = "https://1c54473b-be6e-42d6-b914-d0ecae937981-bluemix.cloudant.com/etablissements";
var query = {
"selector": {
"cp": 24000
}
};
$.ajax({
type: "POST",
url: urlPrefix + '/_find',
contentType: "application/json",
data:JSON.stringify(query),
dataType: 'json',
crossDomain: true,
username: "xxxxxxx-xxxxxxx-42d6-xxxxxx-xxxxxx-bluemix",
password:"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
xhrFields: {
'withCredentials': true // tell the client to send the cookies if any for the requested domain
},
success: function(data) {
console.log(data);
},
error:function(data){
console.log(data);
}
});
</script>
</body>
</html>
编辑 3:已解决!
在此页面的帮助下,我已成功使其工作: https://zinoui.com/blog/ajax-basic-authentication
所以这是我的最终代码,在 firefox、chrome 和 opera 中进行了测试,终于可以正常工作了!所以我现在可以在 cloudant 上启用 CORS 进行 _find 查询,我喜欢它!
请注意,解决方案的关键是 jquery beforeSend() 函数n,您需要在此处提供 凭据:
<html><meta charset="utf-8" />
<head>
<title>JS Bin</title>
</head>
<body>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous" charset="utf-8"></script>
<script>
var urlPrefix = "https://1c54473b-be6e-42d6-b914-d0ecae937981-bluemix.cloudant.com/etablissements";
var USERNAME = "xxxxxxxx-xxxxxxxx-42d6-b914-d0ecae937981-bluemix";
var PASSWORD = "xxxxxxxxxxxxxxxxxb9e74af7368b21a37b531aae819929d3405c7d22";
var query = {
"selector": {
"cp": 24000
}
};
$.ajax({
type: "POST",
contentType: "application/json",
data:JSON.stringify(query),
dataType: 'json',
crossDomain: true,
xhrFields: {
'withCredentials': true // tell the client to send the cookies if any for the requested domain
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(USERNAME + ":" + PASSWORD));
},
url: urlPrefix + '/_find',
success: function(data) {
console.log(data);
},
error:function(data){
console.log(data);
}
});
</script>
</body>
</html>
使用 CORS 应用程序对于构建快速实验室应用程序非常有用!我喜欢这种无服务器技术,请不要停止!
【问题讨论】:
-
我不知道你是否知道,但 PouchDB 将高度简化对 cloudant 的访问。另外,您请求的 http 响应是什么?你可以在开发控制台中看到
-
你好 Alexis,是的,我喜欢 pouchdb 技术,但我愿意在使用 pouchdb 之前让 couchdb 100% 工作。谢谢你 !在这种情况下,根本没有http响应,只显示jquery错误
-
我暂时不想使用 pouchdb,我不想获得本地复制。
-
您可以使用 PouchDB 作为远程数据库的客户端。不需要本地数据库
-
真的吗?哇,这是一个很好的信息 ure 给我!
标签: jquery cors couchdb cloudant