Couchdb jQuery API添加授权标头 [英] Couchdb jquery api add Authorization Header

查看:81
本文介绍了Couchdb jQuery API添加授权标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将授权标头添加到登录请求中. 我尝试使用代码来做到这一点:

I want add the Authorization Header to the login request. I try to do that with the code:

function dblogin (credential){
            $.couch.login({
                name:credential.name, 
                password:credential.password,
                headers: {'Authorization': 'Basic ...'},
                success: function (data){console.log("loggato con successo!");},
                error: function(status) {console.log("login non riuscito!" + status);}
            });
        }

但是我看到HTTP请求标头中没有授权字段.为什么?

But I see that into HTTP request header the field Authorization is not present. Why?

推荐答案

来自

From the source code of jquery.couch.js, you'll see that the login function doesn't use every parameter you send it:

login: function(options) {
  options = options || {};
  $.ajax({
    type: "POST", url: this.urlPrefix + "/_session", dataType: "json",
    data: {name: options.name, password: options.password},
    beforeSend: function(xhr) {
        xhr.setRequestHeader('Accept', 'application/json');
    },
    complete: function(req) {
      var resp = $.parseJSON(req.responseText);
      if (req.status == 200) {
        if (options.success) options.success(resp);
      } else if (options.error) {
        options.error(req.status, resp.error, resp.reason);
      } else {
        throw 'An error occurred logging in: ' + resp.reason;
      }
    }
  });

因此实际上没有办法向其发送自定义标头.

So there's actually no way to send it custom headers.

这实际上是一个耻辱,因为他们定义了

And that's actually a shame, since they define their own ajax function wrapper which allows header customization. But for some reason they don't use it in some particular functions, including login...

这篇关于Couchdb jQuery API添加授权标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆