Axios - 如何阅读JSON响应? [英] Axios - How to read JSON response?

查看:441
本文介绍了Axios - 如何阅读JSON响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Axios 0.17.1

Axios 0.17.1

.then(function (response) {
                console.log(response);
                //console.log(response.status);
                //It is an error -> SyntaxError: Unexpected token u in JSON at position 0 
                console.log(JSON.parse(response.data.error));
                console.log(response.data.error); //undefined.

console.log的回复是

The console.log of response is


{data:{error:名称必须输入不止一个......NULL↵

[isPipe:protected] =>↵NULL↵}↵}↵,状态:203,statusText:
非权威信息,标题: {...},config:{...},...} config
:{adapter:ƒ,transformRequest:{...},transformResponse:{...},timeout:
0,xsrfCookieName:XSRF-TOKEN ,...} data:{error:名称必须是带有多个字符的
。} object(Slim\Http\Response)#32
(5) {↵[status:protected] =>↵int(200)↵
[reasonPhrase:protected] =>↵string(0)↵
[protocolVersion:protected] = >↵string(3)1.1↵
[headers:protected] =>↵对象(Slim \Http \Headers)#33(1){↵

[ data:protected] =>↵array(1){↵[content-type] =>↵

array(2){↵[value] =>↵array(1){ ↵[0] =>↵

string(24)text / html; charset = UTF-8↵}↵

[originalKey] =>↵字符串(12)内容类型↵}↵}↵}↵
[body:protected ] =>↵对象(Slim\Http\Body)#31(7){↵

[stream:protected] =>↵资源(59)类型(流)↵

[meta:protected] =>↵NULL↵[可读:受保护] =>↵NULL↵

[可写:受保护] =>↵NULL↵[ seekable:protected] =>↵
$ b $bNULL↵[size:protected] =>↵NULL↵[isPipe:protected] =>↵
$ b $bNULL↵}↵ }↵headers:{content-type:
application / json; charset = utf-8} request:XMLHttpRequest
{onreadystatechange:ƒ,readyState:4,timeout:0,withCredentials:
false,upload:XMLHttpRequestUpload,...} status:203 statusText:
非权威信息
proto :对象

JSON.parse(response.data)以及response.data.error - >两者都给出错误。我如何阅读数据?

JSON.parse(response.data) as well as response.data.error -> Both are giving error. How can i read the data?

Slimframework 3.

Slimframework 3.

$data = array('error' => 'Name must be entered with more than one character.');
        $newResponse = $response->withJson($data, 203);
        return $newResponse;


推荐答案

在Axios中,响应已经作为javascript对象提供,没有需要解析,只需获得响应和访问数据。

In Axios responses are already served as javascript object, no need to parse, simply get response and access data.

这篇关于Axios - 如何阅读JSON响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆