WordPress函数:wp_send_json_success 发送ajax成功结果

江河/ 2023年10月31日/ WordPress/ 浏览 479

函数原型:


wp_send_json_success( mixed $data = null, int $status_code = null, int $options )


将JSON响应发送回Ajax请求,表示成功。


响应对象将始终具有一个值为true的成功密钥。如果有任何东西被传递给函数,它将被编码为数据键的值。


以下示例数组被转换为JSON:


$response = array( 'success' => true );                   //if $data is empty
$response = array( 'success' => true, 'data' => $data );  //if $data is set


参数说明:


$data,将数据编码为JSON,然后打印并结束程序。


$status_code,HTTP CODE


函数源码:


function wp_send_json_success( $data = null, $status_code = null, $options = 0 ) {
	$response = array( 'success' => true );

	if ( isset( $data ) ) {
		$response['data'] = $data;
	}

	wp_send_json( $response, $status_code, $options );
}


使用举例:


wp_send_json_success( array( 
    'name' => 'Andrew', 
    'call' => 'From some API/trigger', 
    'variable' => $var,
), 200 );


发表评论

暂无评论,抢个沙发...

客服 工单