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

查看:50
本文介绍了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:非权威信息",标题:{…},配置:{…},…} 配置:{适配器:ƒ,转换请求:{…},转换响应:{…},超时:0, xsrfCookieName: "XSRF-TOKEN", ...} data : "{"error":"Name must be输入了多个字符."}object(SlimHttpResponse)#32(5) {↵ ["status":protected]=>↵ int(200)↵["reasonPhrase":protected]=>↵ string(0) ""↵["protocolVersion":protected]=>↵ string(3) "1.1"↵["headers":protected]=>↵ object(SlimHttpHeaders)#33 (1) {↵
["数据":受保护]=>↵ 数组(1) {↵ ["内容类型"]=>↵
数组(2) {↵ ["值"]=>↵ 数组(1) {↵ [0]=>↵
string(24) "text/html; charset=UTF-8"↵ }↵
["originalKey"]=>↵ string(12) "Content-Type"↵ }↵ }↵ }↵["body":protected]=>↵ object(SlimHttpBody)#31 (7) {↵
["stream":protected]=>↵资源(59)类型(流)↵
["meta":protected]=>↵ NULL↵ ["可读":protected]=>↵ NULL↵
[可写":受保护]=>↵ NULL↵ [可写":受保护]=>↵
NULL↵ ["size":protected]=>↵ NULL↵ ["isPipe":protected]=>↵
NULL↵ }↵}↵" 标题:{内容类型:"application/json;charset=utf-8"} 请求:XMLHttpRequest{onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials:假,上传:XMLHttpRequestUpload,...} 状态:203 statusText:非权威信息"proto:对象

{data: "{"error":"Name must be entered with more than one … NULL↵
["isPipe":protected]=>↵ NULL↵ }↵}↵", status: 203, statusText: "Non-Authoritative Information", headers: {…}, config: {…}, …} config : {adapter: ƒ, transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", …} data : "{"error":"Name must be entered with more than one character."}object(SlimHttpResponse)#32 (5) {↵ ["status":protected]=>↵ int(200)↵ ["reasonPhrase":protected]=>↵ string(0) ""↵ ["protocolVersion":protected]=>↵ string(3) "1.1"↵ ["headers":protected]=>↵ object(SlimHttpHeaders)#33 (1) {↵
["data":protected]=>↵ array(1) {↵ ["content-type"]=>↵
array(2) {↵ ["value"]=>↵ array(1) {↵ [0]=>↵
string(24) "text/html; charset=UTF-8"↵ }↵
["originalKey"]=>↵ string(12) "Content-Type"↵ }↵ }↵ }↵ ["body":protected]=>↵ object(SlimHttpBody)#31 (7) {↵
["stream":protected]=>↵ resource(59) of type (stream)↵
["meta":protected]=>↵ NULL↵ ["readable":protected]=>↵ NULL↵
["writable":protected]=>↵ NULL↵ ["seekable":protected]=>↵
NULL↵ ["size":protected]=>↵ NULL↵ ["isPipe":protected]=>↵
NULL↵ }↵}↵" headers : {content-type: "application/json;charset=utf-8"} request : XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …} status : 203 statusText : "Non-Authoritative Information" proto : Object

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天全站免登陆