我使用cloudant,启用了CORS,所有查询都在运行,_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应用程序对于构建快速labos应用程序非常有用!我喜欢这个没有服务器技术,请不要停止这个!!
答案 0 :(得分:0)
在调试这些问题时,我首先会检查您的查询是从Cloudant Dashboard还是从命令行运行:
curl -X POST \
-d'{"selector": { "cp": 24000}}' \
-H'Content-type: application/json' \
"https://USERNAME:PASSWORD@HOST.cloudant.com/etablissements/_find"
答案是“是的,它有效!”。您将获得一个包含{"docs": [ ... ]}
的文档数组的对象。
所以我开始测试你的jQuery代码,如果你删除了}
函数中的流氓success
,它就可以了:
<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://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);
}
});
</script>
</body>
</html>
N.B请不要在公共域中发布您的Cloudant凭证。