You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I then don't want the return to go through the "success" handler, but an "error" handler. I see that the xhr.status mock is set to 0, but cannot see how I could pick up the response status code and use that.
Cheers,
Diego
The text was updated successfully, but these errors were encountered:
So, the author has said in other forums that it's impossible to inspect the HTTP status code of an iframe via javascript. So, I developed a work-around in Rails.
In my controller, I have something like this:
respond_to do |format|
if @object.save
format.js { render :partial => "success_partial", :status => :created }
else
format.js { render :json => @object.errors, :status => :unprocessable_entity }
end
end
Notice if the save succeeds without errors, the app returns the HTML that's in the success_partial along with a successful :created (201) HTTP status code. If the save fails with errors, the app returns a JSON hash of the error messages with a 422 status code (which the jquery form plugin can't see because it goes to the iframe and can't be recovered).
So, in my javascript response-handling code (this is for Rails 3 using the rails.js jQuery adapter), I test the actual response and manually trigger the ajax:failure if the response is able to be parsed as JSON:
$("#my-form")
.bind("ajax:success", function(evt, data, status, xhr){
try{
$.parseJSON(data); // throws error if data is not JSON (meaning success)
$(this).trigger('ajax:failure', xhr, status, data) // only gets to here if JSON parsing was successful (meaning data is error messages)
}catch(err){
// do nothing
}
// do some success handling
})
.bind("ajax:failure", function(evt, xhr, status, error){
// do some error handling
})
This is pretty much what we ended up doing: returning something that signalled an error in a successful response, which we then parsed and handled accordingly.
Hi,
I was wondering if it would be possible to detect a return HTTP return status code upon doing a file upload?
For example, in Rails I may do the following if there was some error during upload:
render :json => @Product, :status => 400
I then don't want the return to go through the "success" handler, but an "error" handler. I see that the xhr.status mock is set to 0, but cannot see how I could pick up the response status code and use that.
Cheers,
Diego
The text was updated successfully, but these errors were encountered: