当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - BackBone js - How to set contentType when making AJAX Call?

问题描述:

In my application - I make an AJAX call to fetch image/binary data.

Question is how to set "application/octet-stream" as content type when making fetch() from my model ?

My Model -- I cant change fetch of "BLA" defined. How to set the CONTENT TYPE through my model ?

define(function(require) {

var bla= require("bla");

return BLA.Model.extend({

__name__: 'xyz',

url: function() {

console.log(this.attributes);

return SOME_URL + this.get('someAttribute');

},

initialize: function(options) {

this.options = options;

this.promise = this.fetch(options);

}

});

});

网友答案:

Backbone.Model.fetch uses Backbone.Sync which uses $.ajax to request the remote server. When you calling Model.fetch(options) , options hash will be passed to $.ajax.

In your case you could just set:

this.promise = this.fetch({ beforeSend: function (request)
            {
                request.setRequestHeader("Content-Type", "application/octet-stream");
            } 
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: