Recently I began to study the backbone framework. I can’t understand how to send images to the server, the request header of such application/json . How to change or change?

    1 answer 1

    I upload the file to the server directly, not from the model. Here is a sample code.

     var classView = Backbone.View.extend({ ... // Bind event click to button events: { 'click .button-upload': 'onUpload' }, // Handler button click onUpload: function(e) { var options = {}; e.preventDefault(); // Get file with Input File var file = this.$('input[type="file"]').get(0).files[0]; // Create FormData var formData = new FormData(); // Append file to FormData object formData.append('file', file); // Call trigger before upload this.trigger('upload:before', options); // Send file to server Bacbone .ajax(_.extend({ url: '/upload', type: 'POST', cache: false, contentType: false, processData: false, data: formData, }, options)) .then(_.bind(function(response) { // success this.trigger('upload:success', response); }, function(responce) { // error this.trigger('upload:error', response); }); } ... }); new view = new classView(); // your other code 

    PS code is written from memory, but in general the idea should be clear.

    PSS It is also possible to alter this code for the model. If need be, write it down, I will help.