wp_send_json_error()
wp_send_json_error( mixed $data = null, int $status_cod…
wp_send_json_error( mixed $data = null, int $status_code = null )
向Ajax请求发送JSON响应,指示失败。
Send a JSON response back to an Ajax request, indicating failure.
目录锚点:#说明#参数#源码#笔记
说明(Description)
如果$data参数是WP_Error对象,则对象中的错误将被处理并作为错误代码和相应消息的数组输出。所有其他类型的输出无需进一步处理。
参数(Parameters)
| 参数 | 类型 | 说明 |
|---|---|---|
| $data | (mixed) | 将数据编码为JSON,然后打印并消亡。 |
| $status_code | (int) | 要输出的HTTP状态代码。 |
源码(Source)
/**
* Send a JSON response back to an Ajax request, indicating failure.
*
* If the `$data` parameter is a {@see WP_Error} object, the errors
* within the object are processed and output as an array of error
* codes and corresponding messages. All other types are output
* without further processing.
*
* @since 3.5.0
* @since 4.1.0 The `$data` parameter is now processed if a {@see WP_Error}
* object is passed in.
*
* @param mixed $data Data to encode as JSON, then print and die.
*/
function wp_send_json_error( $data = null ) {
$response = array( 'success' => false );
if ( isset( $data ) ) {
if ( is_wp_error( $data ) ) {
$result = array();
foreach ( $data->errors as $code => $messages ) {
foreach ( $messages as $message ) {
$result[] = array( 'code' => $code, 'message' => $message );
}
}
$response['data'] = $result;
} else {
$response['data'] = $data;
}
}
wp_send_json( $response );
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 4.7.0 | wp-includes/functions.php | 13 | 20 |
笔记(Notes)
Example
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。





还没有任何评论,赶紧来占个楼吧!