【问题标题】:_find query not working in ibm cloudant_find 查询在 ibm cloudant 中不起作用
【发布时间】: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


【解决方案1】:

在调试此类瘦身时,我会首先检查您的查询是从 Cloudant 仪表板还是从命令行有效:

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>

注意,请不要在公共域中发布您的 Cloudant 凭据。

【讨论】:

  • 您好,我正在尝试您的 jquery 代码,并在我的主要评论中发布了我的错误,谢谢。我正在使用 Firefox 最新版本。括号是复制粘贴错误,但在原始代码中不存在。我让 jquery 抛出错误,并且没有触发 ajax 查询。我正在使用您的代码添加一个带有错误抛出的屏幕副本。我正在通过 3g 手机连接,但其他查询没有问题
  • 这对我来说确实是个大问题,因为这意味着我无法使用 _find 查询 cloudant 数据库,而它在我的 localhost couchdb 上运行良好。
  • 我想留下我的凭据,这是一个测试环境,我希望有人测试并告诉我为什么它不工作,谢谢
  • 我已经测试了上面的代码,它对您的 Cloudant 服务运行良好。
  • 谢谢,你用 firefox 还是 chrome 试试?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-08
  • 2016-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多