[英]WP Show wp die error as an alert
I have the following function in a project im working on and everything up to now works well apart from im stuck with showing error message in alert popup when the function die. 我在一个正在处理的项目中具有以下功能,到目前为止,除了该功能终止后,我一直无法在警报弹出窗口中显示错误消息之外,其他一切工作都很好。
This is Ajax Script 这是Ajax脚本
jQuery(window).load(function() {
action = jQuery.QueryString["action"];
collection_id = jQuery.QueryString["cc"];
current_userid = jQuery.QueryString["cu"];
user_name = jQuery.QueryString["su"];
collection_name = jQuery.QueryString["cn"];
cbcolor = jQuery.QueryString["ccol"];
permission = jQuery.QueryString["permission"];
if(action == 'sharecookbook' && jQuery('body').hasClass('logged-in')) {
new jQuery.flavr({
content : 'Do you really want to add this collection?',
dialog : 'confirm',
onConfirm : function(){
jQuery.ajax({
url: wpb_ajax_url,
data: 'action=wpb_sharecollection&collection_id='+collection_id+'¤t_userid='+current_userid+'&user_name='+user_name+'&collection_name='+collection_name+'&cbcolor='+cbcolor+'&permission='+permission+'&nonce='+ajax_object.nonce,
dataType: 'JSON',
type: 'POST',
success:function(data){
window.location = "/";
}
});
},
});
}
});
Then the following is the ajax php funtion 然后以下是ajax php功能
/* share new collection */
add_action('wp_ajax_nopriv_wpb_sharecollection', 'wpb_sharecollection');
add_action('wp_ajax_wpb_sharecollection', 'wpb_sharecollection');
function wpb_sharecollection(){
global $wpb;
$output = '';
extract($_POST);
check_ajax_referer( 'ajax_form_nonce', 'nonce' );
$wpb->share_collection( $current_userid,$collection_id,$collection_name,$cbcolor,$user_name,$permission );
wp_send_json ( $output );
}
And the following is the share collection function defined in above php funtion 以下是上面的php函数中定义的共享收集功能
/* Share collection */
function share_collection($uid,$id,$name,$cbcolor,$user,$permission) {
$cbcolor = str_replace(" ", "#", $cbcolor);
$type = 'shared';
$user_id = get_user_by( 'email', $user );
$collections = $this->get_collections($user_id->ID);
$collectionsdata = $this->get_collectionsdata($user_id->ID);
$collectionsdataoriginal = $this->get_collectionsdata($uid);
$collectionsdatashared = $this->get_collectionsdatashared($uid);
foreach($collectionsdata as $j => $ajj) {
if ( isset($ajj['parent']) && $ajj['parent'] == $id && isset($ajj['owner']) && $ajj['owner'] == $uid ) {
wp_die( 'This is an error', 'Error' );
}
}
$collections[] = array();
$collectionsdata[] = array('label' => $name,'cbcolor' => $cbcolor,'owner' => $uid,'parent'=>$id,'type' => $type,'permission' => $permission);
$collectionsdataoriginal[$id] = array('label' => $name,'cbcolor' => $cbcolor,'owner' => $uid,'type' => $type);
$idx = count($collectionsdata) - 1;
$collectionsdatashared[] = array('myid'=>$id,'suser' => $user_id->ID,'child'=>$idx);
update_user_meta($user_id->ID, '_wpb_collections', $collections);
update_user_meta($user_id->ID, '_wpb_collectionsdata', $collectionsdata);
update_user_meta($uid, '_wpb_collectionsdata', $collectionsdataoriginal);
update_user_meta($uid, '_wpb_collectionsdatashared', $collectionsdatashared);
}
In the last code you can see there is wp_die defined with error message. 在最后的代码中,您可以看到有错误消息定义的wp_die。 I need to alert this when the function dies to the user.
当函数对用户死亡时,我需要提醒这一点。 Can you suggest a way to do this.
您能建议一种方法吗?
Thanks in advance 提前致谢
Codex says that wp_die() function returns 500 HTML status code. Codex说wp_die()函数返回500个HTML状态代码。 In that case if the request fails use
qXHR.fail()
method ( or error callback, although is deprecated ), to catch error: 在这种情况下,如果请求失败,请使用
qXHR.fail()
方法(或错误回调,尽管已弃用),以捕获错误:
$.ajax({ /* options */ })
.done( function( data, textStatus, jqXHR ) {
alert( "success" );
})
.fail( function( jqXHR, textStatus, errorThrown ) {
alert( "error" );
});
EDIT: 编辑:
I've done some testing and wp_die()
returns 500 status, but not for ajax calls. 我已经做过一些测试,
wp_die()
返回500状态,但没有针对ajax调用。 For ajax it just prints response message and die. 对于ajax,它仅打印响应消息而死。 Here are few scenarios.
这里有几种情况。
Calling wp_die()
with plain string message 用纯字符串消息调用
wp_die()
wp_die( 'Error message', 'Error' );
Status code is 200, but jQuery calls .fail()
callback with parsererror
message. 状态代码为200,但是jQuery调用带有
parsererror
消息的.fail()
回调。 jQuery expects json
( not text/html
that wp_die()
returns ) as defined in datatype
option. jQuery期望在
datatype
选项中定义json
(而不是wp_die()
返回的text/html
wp_die()
。 If you remove datatype option, jQuery will try to guess the data type based on the MIME type of the response. 如果删除datatype选项,则jQuery将尝试根据响应的MIME类型猜测数据类型。
wp_send_json()
sets the Content-Type of the response to application/json
. wp_send_json()
将响应的Content-Type设置为application/json
。
Calling wp_die()
with json data, or calling wp_send_json()
调用
wp_die()
与JSON数据,或者调用wp_send_json()
wp_die( wp_json_encode( array( 'status' => 'error', 'message' => 'Error message' ) ) ); // text/html;
wp_send_json( array( 'status' => 'success', 'data' => 'output' ) ); // application/json
.done()
callback is invoked and json data are passed, the only difference is that later function sets the Content-Type to application/json
, but jQuery will try to parse both responses as json since the datatype option is set to json. 调用
.done()
回调并传递json数据,唯一的区别是以后的函数将Content-Type设置为application/json
,但是jQuery会尝试将两个响应都解析为json,因为datatype选项设置为json。
Configure Wordpress to send 500 status for ajax requests 配置Wordpress以发送500状态以处理Ajax请求
Wordpress can be configured to send 500 status for ajax requests with wp_die_ajax_handler
filter. 可以使用
wp_die_ajax_handler
过滤器将Wordpress配置为发送500状态以处理ajax请求。
// example: wp_die( 'Error message', 'Error', 500 );
add_filter( 'wp_die_ajax_handler', function( $handler ) {
return function( $message, $title, $args ) use ( $handler ) {
if ( isset( $args['response'] ) && $args['response'] == 500 ) {
header( "HTTP/1.1 500 $title" );
die( $message );
}
$handler( $message );
};
});
Solution without wp_die()
functions 没有
wp_die()
函数的解决方案
Simple way without wp_die()
would be to set the success property in json response, and to check that property in .done() callback. 没有
wp_die()
简单方法是在json响应中设置成功属性,并在.done()回调中检查该属性。
// php
wp_send_json( array( 'success' => true, 'data' => 'output' ) ); // for success
wp_send_json( array( 'success' => false, 'data' => 'Error message' ) ); // for failure
// js
jqXHR.done( function( data )
{
if ( data.success ) console.log( data.data );
if ( !data.success ) console.log( data.data );
});
wp_send_json_error()
function wp_send_json_error()
函数
The above is implemented in Wordpress with wp_send_json_error()
function. 以上是在Wordpress中使用
wp_send_json_error()
函数实现的。
// sets a JSON response as array( 'success' => false, 'data' => 'Error message' ), then calls wp_send_json() and wp_die().
wp_send_json_error( 'Error message' );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.