返回介绍

wp_send_json_error()

发布于 2017-09-11 12:49:17 字数 6646 浏览 1249 评论 0 收藏 0

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

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


description

If the $data parameter is a 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.


参数

$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_error( $data = null, $status_code = 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, $status_code );
}

更新日志

Versiondescription
4.7.0The $status_code parameter was added.
4.1.0The $data parameter is now processed if a WP_Error object is passed in.
3.5.0Introduced.

相关函数

Uses

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

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-includes/customize/class-wp-customize-selective-refresh.php: WP_Customize_Selective_Refresh::handle_render_partials_request()
  • 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_heartbeat()
  • 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_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 36 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

    Example

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

    save_field.php

    
    <?php
    $nonce = $_POST['_wpnonce_name'];
    if ( empty( $_POST ) || ! wp_verify_nonce( $nonce, 'my-nonce' ) ) {
    	wp_send_json_error(); // sends json_encoded success=false
    }
    

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

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

发布评论

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