开发者

Rails returning 401 leads to a http auth-type login dropdown window

I'm doing user auth via an Ajax post-request in Rails, and I thought that returning a 401 status and using the error-function in jQuery.ajax() would be a nice way of handling failed authorizations.

However: in Chrome, Safari and FireFox I get one of those standard http auth login windows. Like this: http://bayimg.com/PAENeAAdE (pardon the swedish).

After clicking cancel in the login window, my jQuery error handling function executes as usual.

Is there some way of preventing that login-window from appearing?

I'm using WEBrick 1.3.1, Rails 3.0.1, Ruby 1.8.7.

Code:

application.js

var $form = $("#loginForm");
$form.submit(function(e) {
  e.preventDefault();
  开发者_StackOverflow$.ajax(
    $form.attr('action'), 
    {
      type: "POST",
      data: $form.serialize(), 
      dataType: "json",
      success: function(data) {
          console.debug(data);
          $("#login").append("Inloggningen lyckades!");
          var panel = ich.userPanel({"name":data.user.email});
          $("#header").html(panel);     
      },
      error: function(header, status, error){
        alert("I am handling errors");
      }
    })
})

sessions_controller.rb:

def create 
  user = User.authenticate(params[:email], params[:password])
  respond_to do |format|

    # Some misc html-stuff

    format.json {
      if user
        session[:email] = user.email
        render :json => user
      else
        render :json => "Fel lösenord eller adress.", :status => :unauthorized
      end
    }
  end
end


Don't return a 401. 401 is used exclusively with http authentication.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜