Javascript Axios - 如何读取 JSON 响应?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48062821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Axios - How to read JSON response?
提问by Mahesh
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.
The console.log of response is
响应的 console.log 是
{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(Slim\Http\Response)#32 (5) {? ["status":protected]=>? int(200)? ["reasonPhrase":protected]=>? string(0) ""? ["protocolVersion":protected]=>? string(3) "1.1"? ["headers":protected]=>? object(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"]=>? string(12) "Content-Type"? }? }? }? ["body":protected]=>? object(Slim\Http\Body)#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
{data: "{"error":"必须输入一个以上的名称……NULL?
[“isPipe”:受保护]=>?空值?}?}?", status: 203, statusText: "Non-Authoritative Information", headers: {…}, config: {…}, …} config: {adapter: ?, transformRequest: {…}, transformResponse: {…} }, timeout: 0, xsrfCookieName: "XSRF-TOKEN", …} data : "{"error":"Name 必须输入一个以上字符。"}object(Slim\Http\Response)#32 (5) {? [“状态”:受保护]=>?整数(200)?["reasonPhrase":protected]=>? 字符串(0)“”?[“协议版本”:受保护]=>?字符串(3)“1.1”?[“标题”:受保护]=>?object(Slim\Http\Headers)#33 (1) {?
[“数据”:受保护]=> ? 数组(1){?[“内容类型”]=>?
数组(2){?[“价值”]=>?数组(1){?[0]=>?
字符串(24)“文本/html;字符集=UTF-8”?}?
[“原始密钥”]=>?字符串(12)“内容类型”?}? }? }? [“身体”:受保护]=>?object(Slim\Http\Body)#31 (7) {?
[“流”:受保护]=>?类型(流)的资源(59)?
[“元”:受保护]=>?空值?[“可读”:受保护]=>?空值?
[“可写”:受保护]=>?空值?["seekable":protected]=>?
空值?[“大小”:受保护]=>?空值?[“isPipe”:受保护]=>?
空值?}?}?" 标头:{content-type: "application/json;charset=utf-8"} 请求:XMLHttpRequest {onreadystatechange: ?, readyState: 4, timeout: 0, withCredentials: false,
JSON.parse(response.data) as well as response.data.error -> Both are giving error. How can i read the data?
JSON.parse(response.data) 以及 response.data.error -> 两者都给出错误。我怎样才能读取数据?
Slimframework 3.
3. 瘦身框架
$data = array('error' => 'Name must be entered with more than one character.');
$newResponse = $response->withJson($data, 203);
return $newResponse;
回答by Mosè Raguzzini
In Axios responses are already served as javascript object, no need to parse, simply get response and access data.
在 Axios 中,响应已经作为 javascript 对象提供,无需解析,只需获取响应并访问数据。
回答by Kleo
I had a similar format response as the one in console log and my issue was that my .json file wasn't properly formatted. I was missing a comma. Post your json file to have a look.
我有一个与控制台日志中类似的格式响应,我的问题是我的 .json 文件格式不正确。我少了一个逗号。发布您的 json 文件以查看。

