We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
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>
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.
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
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??
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?
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?
Any suggestions?
Hey Yash,
Seems like that should work.
What version of Backbone are you using? And can you inspect the headers of the response?
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
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");
}
});
Thanks, works great with Backbone.js
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?
Did you try enabling CORS? http://enable-cors.org/
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?
Yes, exactly.
That's the response the server should send.
Ok, thanks. I thought so but it's weird to think of wrapping a server response with an HTML tag :)
Thank You for this great post.. It worked like charm
Thanks man works very well and easy
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