Javascript 将自定义错误代码传递给ajax错误函数
我正在尝试将自定义错误代码传递给客户端to ajax错误函数。 在服务器端:Javascript 将自定义错误代码传递给ajax错误函数,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我正在尝试将自定义错误代码传递给客户端to ajax错误函数。 在服务器端: $response = array(); if ( empty($post['parent_id']) ) { $response = array('error' => true, 'status_code' => -2); exit(); } $is_valid_id = RC()->is_valid_id($post['parent_id']); $row = RC()->
$response = array();
if ( empty($post['parent_id']) ) {
$response = array('error' => true, 'status_code' => -2);
exit();
}
$is_valid_id = RC()->is_valid_id($post['parent_id']);
$row = RC()->get_row_data($post['parent_id']);
if ( ! $is_valid_id ) {
$response = array('error' => true, 'status_code' => -1);
} else if ( ! $row ) {
$response = array('error' => true, 'status_code' => 0);
} else {
$response = json_encode($row);
}
echo $response;
然后我想在我的js脚本中检查此状态代码,但找不到执行此操作的方法(只找到没有触发错误事件的方法)
任何帮助都将不胜感激。这是因为响应将转到您的响应回调,您正在成功返回一个对象 只有当请求本身失败时(超时、404等),才会调用错误回调
您需要在成功回调中处理内部错误代码这是因为响应将转到您的响应回调,您正在成功返回一个对象 只有当请求本身失败时(超时、404等),才会调用错误回调
您需要在成功回调中处理内部错误代码响应确实会触发错误函数,但是属性
status\u code
为空。这是因为您没有正确返回它-在代码上-2您没有返回任何内容-是什么导致超时,对于其他2个错误,可能是因为您没有解析它-简单地向对象添加一个错误键true不会使您的响应转到错误回调OK,我在成功回调中处理错误。尽管对错误使用错误回调更有意义。如果您确实坚持要更改响应状态以访问错误回调函数,则响应会触发错误函数,但是属性status\u code
是空的。这是因为您没有正确返回它-在代码-2中,您没有返回任何内容-是什么导致超时,而在其他2个错误中,可能是因为您没有解析它-仅向对象添加错误键true不会使您的响应转到错误回调OK,我在成功回调中处理错误。尽管对错误使用错误回调更有意义。如果你真的坚持,你可以更改响应状态以获得错误回调函数
$.ajax({
url: ajax_url,
data: {
'action': 'rc_parent_sign_in',
'form_data': $('#parent-sign-in-form').serialize(),
'security': security_nonce
},
type: "post",
dataType: "json",
cache: false,
success: function (response) {
var query_vars = $.param(response);
window.location.replace('http://localhost/renecassin/user-registration/?' + query_vars);
},
error: function (response) {
$('.form-control-feedback').addClass('hide');
/* Looking for something like this */
switch ( response.status_code) {
case -2 :
parent_id_form_group.addClass('has-danger').children('#empty-field').
removeClass('hide');
prent_id_input.addClass('form-control-danger');
break;
case -1 :
parent_id_form_group.addClass('has-danger').children('#not-valid-id-feedback').
removeClass('hide');
prent_id_input.addClass('form-control-danger');
break;
default :
parent_id_form_group.addClass('has-danger').children('#id-not-exists-feedback').
removeClass('hide');
prent_id_input.addClass('form-control-danger');
}
}
});