We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

thennarasu • 11 years ago

i need full code in zip..where i download ?i need to upload image to web service using Backbone.js its possible or not? any one help me.........
thanks in advance

Cabuxa Mapache • 11 years ago

UUmmm, I've added success and error handlers in model.save and always goes to the error except in IE... any sugestions why??

var options = {
iframe: true,
files: this.$('form :file'),
data: values,
success: function (model, response) {
alert("OK");
},
error: function (model, response) {
alert("Error");
},
wait: true
};

this.model.save(null, options);

The response from the server is:

<textarea data-type="application/json">
{"CodeError": "0", "DescError": "", "Path": "c:\hello\file.txt", "FileName": "go-590x260.jpg", "FileSize": "13983", "Sha1": ""}
</textarea>

ejpastorino • 11 years ago

Looks like the response is OK. That's the only content, no <html> tags or anything else, right?

Also try doing the save with and empty object instead of null as the first parameter.

Cabuxa Mapache • 11 years ago

Hi, thanks for your answer. The problem was in the "Path" field of the JSon. The library "Json2.js" was failing to parse the JSon because of the "\" character... I delete this field and everything is OK ;)
By the way, sendind a empty object in the first parameter of the model.save is not working for me, I'm using Backbone.js 1.1.0

Yash Kochar • 11 years ago

I am getting org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported Exception with Http 500 Internal Error, what will be the possible cause for this??

ejpastorino • 11 years ago

Hey Yash,
I haven't used Spring Framework, but it seems like the application is not recognizing the content type sent.
Maybe it's expecting JSON or something other content header?

Yash Kochar • 11 years ago

Hey, that problem is resolved.

I am now facing new problem. As you said in order to get access to http headers of the server response we need to wrap data around
<textarea data-type="application/json">
{"ok": true, "message": "Thanks so much"}
</textarea>

I did that on server side using
response.getOutputStream().write("<textarea data-type="\"application/json\"">{\"ok\": true, \"message\": \"Thanks so much\"}</textarea>".getBytes());

and i can see Respose when i debugged on Network Tab

But my console.log(data) is returning data object which contains responsetext property as
<textarea data-type="application/json">
{"ok": true, "message": "Thanks so much"}
</textarea>

I am not getting ok and message property, i debugged iframe-transfport.js and found that my root elemnt doesnt contain textarea tag only....

WHAT AM I DOING WRONG?

Yash Kochar • 11 years ago

Any suggestions?

ejpastorino • 11 years ago

Hey Yash,
Seems like that should work.
What version of Backbone are you using? And can you inspect the headers of the response?

Yash Kochar • 11 years ago

Backbone save method doesnt give me success callback also, is there any way to get that working? I am currently using normal jquery. ajax request for success calbacks

Yash Kochar • 11 years ago

Anything I am missing here below, as i am not getting success callback after getting response from server...

platformComponentModel.save(values,{

iframe: true,

files: view.$('#chooseFile'),

data: values,

processData: false

}, {

success: function (model, response, options) {

debugger;

view.trigger(view.EVENT.saveSuccess, model);

view.hide();

},

error: function (model, response, options) {

alert("Error while Uploading the File....Please try again");

}

});

borys13 • 11 years ago

Thanks, works great with Backbone.js

Henrique • 11 years ago

Excellent. This is so tricky that I had a hard time putting all the pieces together, thank you.

EDIT: Unfortunately, the iframe trick didn't worked in my case, because I'm posting the files to an API in another domain/port and the browser blocks that. Any solutions?

Chris • 11 years ago

Did you try enabling CORS? http://enable-cors.org/

tonicboy • 11 years ago

What do you mean when you say "Wrap the data in a <textarea> element with a data-type attribute specifying the content type"? Where does that happen, on the server side?

ejpastorino • 11 years ago

Yes, exactly.
That's the response the server should send.

tonicboy • 11 years ago

Ok, thanks. I thought so but it's weird to think of wrapping a server response with an HTML tag :)

Hk • 12 years ago

Thank You for this great post.. It worked like charm

alebrandspa • 12 years ago

Thanks man works very well and easy