Вопрос по jquery – добавить заголовок запроса на магистраль

49

Мой сервер имеет ручную авторизацию. Мне нужно указать имя пользователя / пароль моего сервера в моем порядке запроса магистрали, чтобы он прошел. Как я могу это сделать? Есть идеи? Спасибо

Ваш Ответ

9   ответов
10
Backbone.$.ajaxSetup({
    headers: {'Authorization' :'Basic USERNAME:PASSWORD'}
});

xhr.setRequestHeader

MyCollection.fetch({ headers: {'Authorization' :'Basic USERNAME:PASSWORD'} } );

MyCollection.fetch();

Error: User Rate Limit Exceededanother answer of mineError: User Rate Limit Exceededthe $.ajaxSetup one above.
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
28

$.ajaxSetup({
    headers: { 'Authorization' :'Basic USERNAME:PASSWORD' }
});

Edit 2nd Jan 2018

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededdon't use thisError: User Rate Limit Exceededthe reasons I described in another answer.
17

#coffeescript
_sync = Backbone.sync
Backbone.sync = (method, model, options) ->
    options.beforeSend = (xhr) ->
        xhr.setRequestHeader('X-Auth-Token_or_other_header' , your_hash_key)
        #make sure your server accepts X-Auth-Token_or_other_header!!
    #calling the original sync function so we only overriding what we need
    _sync.call( this, method, model, options )       
5

Backbone.AuthenticatedModelBackbone.Model.

Backbone.AuthenticatedModel = Backbone.Model.extend({
    sync: function(method, collection, options){
        options = options || {};
        options.beforeSend = 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))));
        };
        return Backbone.Model.prototype.sync.apply(this, arguments);
    }

});

Backbone.AuthenticatedModel

var Process = Backbone.AuthenticatedModel.extend({
    url: '/api/process',

});
Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededbeforeSendError: User Rate Limit Exceededbackbonejs.org/docs/backbone.html#section-179
Error: User Rate Limit ExceededBackbone.Model.prototype.sync.apply(this, arguments)Error: User Rate Limit Exceeded
53

fetch, savedestroyBackbone.sync

fetch, savedestroy[options][options]

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

fetch, savedestroy

 fetch({
  beforeSend: sendAuthentication 
 });

Backbone.syncbeforeSend

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededjaphr.blogspot.com/2011/10/…Error: User Rate Limit ExceededoptionsError: User Rate Limit ExceededBackbone.Model.prototype.fetch.call(this, options).
Error: User Rate Limit Exceeded jongbanaag
Error: User Rate Limit Exceeded jongbanaag
Error: User Rate Limit Exceededthis question
-1

$.ajaxSetup({
    xhrFields: {
        withCredentials: true
    },
    async: true
});

In the server side add these headers (PHP):

header('Access-Control-Allow-Origin: http://your-client-app-domain');
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
header('Access-Control-Allow-Credentials: true');
42

MyCollection.fetch( { headers: {'Authorization' :'Basic USERNAME:PASSWORD'} } );
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
3
Object.save(
  {'used': true}
  {headers: {'Access-Token': 'access_token'}}
)
Error: User Rate Limit Exceededfetch example aboveError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
1

    REPORTING_API_KEY = 'secretKeyHere';
    CustomSync = function(method, model, options) {
        options.headers = {
            'Authorization' : 'Bearer ' + REPORTING_API_KEY
        };
        return Backbone.sync(method, model, options);
    };

    MyModel = Backbone.Model.extend({
        urlRoot: '/api/',
        sync: CustomSync
    });
Error: User Rate Limit ExceededsyncError: User Rate Limit ExceededBackbone.sync.

Похожие вопросы