admin管理员组文章数量:1416313
I have a http
request to WP REST API
that can resolve in a success or a failure status
The success gets handled by WP_REST_Response
, and if any error occurs this is handled by WP_Error
, both returned if that's the case
However, in both cases when I receive the server response I don't get any of the messages I've added
public function myFunction($request = null)
{
$response = array();
$parameters = $request->get_json_params();
$error = new WP_Error();
if (success) {
$response['code'] = 200;
$response['message'] = __("Great", "great");
} else {
$error->add(406, __("nope", 'almost'), array('status' => 400));
return $error;
}
return new WP_REST_Response($response, 200);
}
If I inspect the response that I receive, I cannot have access to any message, I only receive
Response {
body: (...)
bodyUsed: true
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
url: "..."
__proto__: Response
}
How does it work?
I have a http
request to WP REST API
that can resolve in a success or a failure status
The success gets handled by WP_REST_Response
, and if any error occurs this is handled by WP_Error
, both returned if that's the case
However, in both cases when I receive the server response I don't get any of the messages I've added
public function myFunction($request = null)
{
$response = array();
$parameters = $request->get_json_params();
$error = new WP_Error();
if (success) {
$response['code'] = 200;
$response['message'] = __("Great", "great");
} else {
$error->add(406, __("nope", 'almost'), array('status' => 400));
return $error;
}
return new WP_REST_Response($response, 200);
}
If I inspect the response that I receive, I cannot have access to any message, I only receive
Response {
body: (...)
bodyUsed: true
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
url: "..."
__proto__: Response
}
How does it work?
Share Improve this question edited Sep 3, 2019 at 15:21 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Sep 3, 2019 at 13:42 GWorkingGWorking 16510 bronze badges1 Answer
Reset to default 1Already found, if I parse the response response.json()
I get the messages in both cases
本文标签: rest apiReturn WPError as WPRESTResponse
版权声明:本文标题:rest api - Return WP_Error as WP_REST_Response 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745189730a2646855.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论