加上骨干请求头 [英] add request header on backbone

查看:95
本文介绍了加上骨干请求头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器具有手动授权。我需要把我的服务器的用户名/密码到我的骨干请求序为它去。请问有什么可以做呢?有任何想法吗?谢谢

My server has a manual authorization. I need to put the username/password of my server to my backbone request inorder for it to go through. How may i do this? Any ideas? Thank you

推荐答案

在骨干模型检索,更新和销毁使用方法获取数据保存摧毁。这些方法委托实际的请求部分Backbone.sync。引擎盖下,所有的 Backbone.sync 做的是创建使用jQuery Ajax请求。为了将你的基本的HTTP认证你有两个选择。

Models in Backbone retrieve, update, and destroy data using the methods fetch, save, and destroy. These methods delegate the actual request portion to Backbone.sync. Under the hood, all Backbone.sync is doing is creating an ajax request using jQuery. In order to incorporate your Basic HTTP authentication you have a couple of options.

保存摧毁都接受额外的参数 [选项] 。这些 [选项] 是简单的jQuery的请求选项的字典,得到纳入jQuery的AJAX调用制成。这意味着您可以轻松定义哪些附加的验证一个简单的方法:

fetch, save, and destroy all accept an additional parameter [options]. These [options] are simply a dictionary of jQuery request options that get included into jQuery ajax call that is made. This means you can easily define a simple method which appends the authentication:

sendAuthentication = function (xhr) {
  var user = "myusername";// your actual username
  var pass = "mypassword";// your actual password
  var token = user.concat(":", pass);
  xhr.setRequestHeader('Authorization', ("Basic ".concat(btoa(token))));
}

和包含在每个保存摧毁打电话给你做。像这样:

And include it in each fetch, save, and destroy call you make. Like so:

 fetch({
  beforeSend: sendAuthentication 
 });

这可以创造相当多的重复。另一种选择可能是覆盖 Backbone.sync 方法,复制原始code,只是包括 beforeSend 选择到每个jQuery的AJAX请求制成。

This can create quite a bit of repetition. Another option could be to override the Backbone.sync method, copy the original code and just include the beforeSend option into each jQuery ajax request that is made.

希望这有助于!

这篇关于加上骨干请求头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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