Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle error return HTTP status codes? #26

Closed
dbarros opened this issue Aug 6, 2010 · 2 comments
Closed

Handle error return HTTP status codes? #26

dbarros opened this issue Aug 6, 2010 · 2 comments
Labels

Comments

@dbarros
Copy link

dbarros commented Aug 6, 2010

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

@JangoSteve
Copy link

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
  })

@dbarros
Copy link
Author

dbarros commented Oct 24, 2010

Hi Steve,

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.

Cheers!

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants