Introduce wp_send_json() to json-encode a variable, print it, and die, for Ajax requests. Effectively replaces WP_Ajax_Response, which was an XML-based response.
Introduce wp_send_json_success() and wp_send_json_error(), which will allow us to fire appropriate done() or fail() callbacks in JS. props koopersmith. see #21390. git-svn-id: http://core.svn.wordpress.org/trunk@21679 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
e286612fda
commit
c8a328f100
|
@ -2142,6 +2142,54 @@ function _scalar_wp_die_handler( $message = '' ) {
|
|||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a JSON response back to an Ajax request.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*
|
||||
* @param mixed $response Variable (usually an array or object) to encode as JSON, then print and die.
|
||||
*/
|
||||
function wp_send_json( $response ) {
|
||||
@header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
|
||||
echo json_encode( $json );
|
||||
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
|
||||
wp_die();
|
||||
else
|
||||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a JSON response back to an Ajax request, indicating success.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*
|
||||
* @param mixed $data Data to encode as JSON, then print and die.
|
||||
*/
|
||||
function wp_send_json_success( $data = null ) {
|
||||
$response = array( 'success' => true );
|
||||
|
||||
if ( isset( $data ) )
|
||||
$response['data'] = $data;
|
||||
|
||||
wp_send_json( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a JSON response back to an Ajax request, indicating failure.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*
|
||||
* @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 ) )
|
||||
$response['data'] = $data;
|
||||
|
||||
wp_send_json( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the WordPress home page URL.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue