页面

时间:2016-03-25 11:30:26

标签: javascript jquery laravel-5

我遇到了一个没有显示错误的问题。

这是我的代码

   $.ajax({
           url: '{{ url("userposteditajax") }}',
          dataType: 'json',
          type: 'post',
          data: {firstname:firstname,lastname:lastname,phone:phone,email:email,address:address,country:country,city:city,password:password,role:role,status:status,id:id},
          success: function( data, textStatus, jQxhr ){
            if(data.message == 'Success')
            {
                $('#ajax_submit').prop('disabled', false);

                location.reload();
                $.magnificPopup.close();
            }
            else {
                var errors = data.message;
                 $('#ajax_submit').prop('disabled', false);

                $.each( errors, function( key, value ) {
                // console.log(errors);
                    $('#ajax_error').fadeIn('slow',function(){

                        $('#ajax_error_mess').html(value);

                    });
                });
            }

            },
            error: function( jqXhr, textStatus, errorThrown ){

                $('#ajax_submit').prop('disabled', false);
                var errors = jqXhr.responseJSON;
                $.each( errors, function( key, value ) {
                    $('#ajax_error').fadeIn('slow',function(){
                        $('#ajax_error_mess').html(value);
                    });
                });
            }
       });
    return false;
   });

我的控制器代码:

 $rules = ['firstname' => 'required|max:20',
               'lastname'  => 'required|max:10',

               'phone'     => 'required|max:15',
               'email'     => 'required|email',
               'address'   => 'required',
               'country'   => 'required',
               'city'      => 'required',
               'role'      => 'required',
               'status'    => 'required'];
            $validator = Validator::make(Input::all(),$rules);
            if( $validator->fails() )
            {           

            $error='';
            $mess = $validator->getMessageBag()->toArray();
            foreach($mess as $row => $val)
             {
                foreach($val as $key )
                {
                    $error .=$key." <br />";
                }
             }

                //return json_encode(array('message'=>$error));
                return Response::json(array('message'=>$error));

            }

即使在控制台中也不会显示错误。

我得到的回应输出是

 {"message":"The firstname field is required. <br \/>The lastname field is required. <br \/>The email field is required. <br \/>The address field is required. <br \/>"}

1 个答案:

答案 0 :(得分:0)

我对您的代码进行了一些修改。

<强> Jquery的:

$.ajax({
    url: '{{ url("userposteditajax") }}',
    dataType: 'json',
    type: 'post',
    data: {firstname:firstname,lastname:lastname,phone:phone,email:email,address:address,country:country,city:city,password:password,role:role,status:status,id:id},
    success: function( data, textStatus, jQxhr ){
        if(data.success === true)
        {
            $('#ajax_submit').prop('disabled', false);

            location.reload();
            $.magnificPopup.close();
        }
        else {
            $.each(data.errors, function (key, value) {
                //print errors in the console
                console.log('Field name: ' + key + ', Error: ' + value);

            }

        },
        error: function( jqXhr, textStatus, errorThrown ){

            $('#ajax_submit').prop('disabled', false);
            var errors = jqXhr.responseJSON;
            $.each( errors, function( key, value ) {
                $('#ajax_error').fadeIn('slow',function(){
                    $('#ajax_error_mess').html(value);
                });
            });
        }
    });

<强> PHP:

$response['success'] = true;

$rules = ['firstname' => 'required|max:20',
        'lastname'  => 'required|max:10',
        'phone'     => 'required|max:15',
        'email'     => 'required|email',
        'address'   => 'required',
        'country'   => 'required',
        'city'      => 'required',
        'role'      => 'required',
        'status'    => 'required'];

$validator = Validator::make(Input::all(),$rules);

if( $validator->fails() )
{           
    $response['success'] = false;
    $response['errors'] = $validator->errors();
    return Response::json($response);
}


return Response::json($response);