[wp-trac] [WordPress Trac] #36261: Allow a response code to be passed to wp_send_json_success, wp_send_json_error and wp_send_json

WordPress Trac noreply at wordpress.org
Wed Mar 16 13:11:08 UTC 2016


#36261: Allow a response code to be passed to wp_send_json_success,
wp_send_json_error and wp_send_json
---------------------------+-----------------------------
 Reporter:  stephenharris  |      Owner:
     Type:  enhancement    |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  REST API       |    Version:  trunk
 Severity:  normal         |   Keywords:
  Focuses:                 |
---------------------------+-----------------------------
 Currently, regardless of whether you call `wp_send_json_success()` or
 `wp_send_json_error()` the HTTP header code is 200. The only difference
 between the two is a 'success' flag which takes a boolean value.

 While this may be appropriate in some cases, it would be useful to be able
 to specify a response code. This is perhaps chiefly relevant to
 `wp_send_json_error()` and by consequently `wp_send_json()`, where the
 client expects error messages to have an appropriate error code (i.e. 4xx
 or 5xx). For example, Backbone allows success and error callbacks to be
 specified when making an ajax request and will invoke one or the other
 depending on the response code.

 Currently this is worked around by checking the success flag in the
 success callback. Allowing a response code to be set means we can:

  1. Keep 'success' behaviour separate from 'error' behaviour as almost all
 HTTP frameworks will automatically handle responses differently depending
 on the response code.
  2. Remove duplication of 'error' behaviour (as we may still need to
 handle errors when a 200 response is returned. In Backbone this means a
 duplication of code in the success and error callbacks.

 The response code would default to 200.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/36261>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list