ajax验证登录未显示

时间:2014-05-15 11:43:19

标签: javascript ajax laravel

我正在使用laravel 4,我正在尝试使用ajax显示登录验证。我有以下javascript验证:

jQuery('#form-signin').submit(function()
{
    var url = $(this).attr("action");
    jQuery.ajax({
        url: url,
        type: "post",
        data: jQuery('#form-signin').serialize(),
        datatype: "json",
        beforeSend: function()
        {
            jQuery('#ajax-loading').show();
            jQuery(".validation-error-inline").hide();
        }
    })
            .done(function(data)
            {
                $('#validation-login').empty()
                if (data.validation_failed === 1)
                {
                    var arr = data.errors;
                    alert(arr);
                }
                else {
                    window.location = data.redirect_to;
                }
            })
            .fail(function(jqXHR, ajaxOptions, thrownError)
            {
                alert('No response from server');
            });
    return false;
});

并在我的userController中:

   public function doLogin() {
        Input::flash();
        $data = [
            "errors" => null
        ];
        if (Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password')))) {
            return Response::json(["redirect_to" => "/"]);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => 'Invalid username or password',
                );
                return Response::json($response_values);
            }else
            {
                echo 'error';
            }
        }
    }

问题是它总是显示“错误”消息,这意味着不执行jaax请求。有什么问题?

1 个答案:

答案 0 :(得分:0)

在doLogin()函数中尝试

return Response::json(array(
    'validation_failed' => 1,
    'errors' => 'Unknow error'
))

而不是

echo "error"