Javascript 页面中未显示错误
我遇到了一个错误不显示的问题 这是我的密码Javascript 页面中未显示错误,javascript,jquery,laravel-5,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
$.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));
}
$rules=['firstname'=>'必需|最大值:20',
'lastname'=>'必填项|最大值:10',
'phone'=>'必填项|最大值:15',
“电子邮件”=>“必需”|电子邮件”,
'地址'=>'必需',
'国家'=>'必需',
“城市”=>“必需”,
'角色'=>'必需',
'状态'=>'必需'];
$validator=validator::make(输入::all(),$rules);
如果($validator->fails())
{
$error='';
$mess=$validator->getMessageBag()->toArray();
foreach($row=>$val)
{
foreach($val作为$key)
{
$error.=$key.“
”;
}
}
//返回json_encode(数组('message'=>$error));
返回响应::json(数组('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 \/>"}
{“message”:“firstname字段是必需的。
lastname字段是必需的。
email字段是必需的。
address字段是必需的。
”
我对您的代码做了一些修改
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);
});
});
}
});
$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);
PHP:
$.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);
});
});
}
});
$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);
我们手上没有魔法球。控制台中有错误吗?html中的值是什么?您是否在其他情况下得到中断?我得到的错误已给出,控制台中未显示任何内容。在$.each(errors,function(key,value){………}之前如果我在控制台中打印数据,那么错误将显示在控制台中。