返回介绍

wp_send_json_success()

发布于 2017-09-11 12:49:24 字数 6111 浏览 1136 评论 0 收藏 0

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

Send a JSON response back to an Ajax request, indicating success.


description


参数

$data

(mixed) (Optional) Data to encode as JSON, then print and die.

Default value: null

$status_code

(int) (Optional) The HTTP status code to output.

Default value: null


源代码

File: wp-includes/functions.php

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

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

	wp_send_json( $response, $status_code );
}

更新日志

Versiondescription
4.7.0The $status_code parameter was added.
3.5.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: wp_send_json()

Used By

  • wp-admin/includes/ajax-actions.php: wp_ajax_get_community_events()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::ajax_insert_auto_draft_post()
  • wp-admin/includes/ajax-actions.php: wp_ajax_search_install_plugins()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_plugin()
  • wp-admin/includes/ajax-actions.php: wp_ajax_search_plugins()
  • wp-admin/includes/ajax-actions.php: wp_ajax_install_theme()
  • wp-admin/includes/ajax-actions.php: wp_ajax_update_theme()
  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_theme()
  • wp-admin/includes/ajax-actions.php: wp_ajax_install_plugin()
  • wp-admin/includes/ajax-actions.php: wp_ajax_get_post_thumbnail_html()
  • wp-includes/customize/class-wp-customize-selective-refresh.php: WP_Customize_Selective_Refresh::handle_render_partials_request()
  • wp-admin/includes/ajax-actions.php: wp_ajax_generate_password()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_wporg_username()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::ajax_load_available_items()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::ajax_search_available_items()
  • wp-admin/includes/ajax-actions.php: wp_ajax_crop_image()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::refresh_nonces()
  • wp-admin/includes/ajax-actions.php: wp_ajax_update_plugin()
  • wp-admin/includes/class-wp-press-this.php: WP_Press_This::add_category()
  • wp-admin/includes/class-wp-press-this.php: WP_Press_This::save_post()
  • wp-admin/includes/ajax-actions.php: wp_ajax_destroy_sessions()
  • wp-admin/custom-background.php: Custom_Background::ajax_background_add()
  • wp-admin/includes/ajax-actions.php: wp_ajax_parse_embed()
  • wp-admin/includes/ajax-actions.php: wp_ajax_parse_media_shortcode()
  • wp-admin/includes/ajax-actions.php: wp_ajax_set_attachment_thumbnail()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_attachment_order()
  • wp-admin/includes/ajax-actions.php: wp_ajax_send_attachment_to_editor()
  • wp-admin/includes/ajax-actions.php: wp_ajax_send_link_to_editor()
  • wp-admin/includes/ajax-actions.php: wp_ajax_get_revision_diffs()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_user_color_scheme()
  • wp-admin/includes/ajax-actions.php: wp_ajax_query_themes()
  • wp-admin/includes/ajax-actions.php: wp_ajax_set_post_thumbnail()
  • wp-admin/includes/ajax-actions.php: wp_ajax_wp_fullscreen_save_post()
  • wp-admin/includes/ajax-actions.php: wp_ajax_get_attachment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_query_attachments()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_attachment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_attachment_compat()
  • wp-admin/includes/ajax-actions.php: wp_ajax_find_posts()
  • wp-admin/custom-header.php: Custom_Image_Header::ajax_header_crop()
  • wp-admin/custom-header.php: Custom_Image_Header::ajax_header_add()
  • wp-admin/custom-header.php: Custom_Image_Header::ajax_header_remove()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::save()
  • wp-includes/class-wp-customize-widgets.php: WP_Customize_Widgets::wp_ajax_update_widget()
  • Show 38 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Basic Example

    
    jQuery( document ).ready( function() {
    
    	jQuery( '#btn_save' ).click( function( e ) {
    		e.preventDefault();
    		jQuery.post( pluginUrl+ 'ajax/save_field.php', 
    			jQuery( '#my-form' ).serialize(), function( data ) {                        
    				alert( data.message ); 
    			}
    		);
    	} );
    } );
    

    save_field.php:

    
    // Bootstrap WP
    
    $return = array(
    	'message' => __( 'Saved', 'textdomain' ),
    	'ID'      => 1
    );
    wp_send_json_success( $return );
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文