Couchdb jquery api add Authorization Header

ⅰ亾dé卋堺 提交于 2019-12-13 04:54:13

问题


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);}
            });
        }

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


回答1:


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.

Edit: 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...



来源:https://stackoverflow.com/questions/19861847/couchdb-jquery-api-add-authorization-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!