Mah*_*esh 12 javascript slim reactjs axios
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.
Run Code Online (Sandbox Code Playgroud)
console.log的响应是
{data:"{"error":"名称必须输入多个...NULL↵
["isPipe":protected] =>↵NULL↵}↵}↵",状态:203,statusText:"非权威信息",header:{...},config:{...},...} config:{adapter:ƒ,transformRequest:{...},transformResponse:{...},timeout:0,xsrfCookieName:"XSRF-TOKEN",...}数据: "{"错误":".名称必须与多于一个的字符来输入"}对象(超薄\ HTTP \响应)#32(5){↵["状态":保护] =>↵INT(200)↵ ["reasonPhrase":protected] =>↵string(0)""↵["protocolVersion":protected] =>↵string(3)"1.1"↵["headers":protected] =>↵对象(Slim\Http \集管)#33(1){↵
[ "数据":保护] =>↵阵列(1){↵[ "内容类型"] =>↵
阵列(2){↵[ "值"] =>↵ array(1){↵[0] =>↵string
(24)"text/html; charset = UTF-8"↵}↵
["originalKey"] =>↵字符串(12)"Content-Type"↵}↵ }↵}↵["body":protected] =>↵对象(Slim\Http\Body)#31(7){↵
["stream":protected] =>↵资源(59)类型(流)↵
[ "元":保护 ED] =>↵NULL↵[ "可读的":保护] =>↵NULL↵
[ "可写":保护] =>↵NULL↵[ "可搜索":保护] =>↵
NULL↵[ "大小":保护] =>↵NULL↵["isPipe":protected] =>
↵NULL↵}↵}↵"headers:{content-type:"application/json; charset = utf-8"} request:XMLHttpRequest {onreadystatechange:ƒ,readyState :4,超时:0,withCredentials:false,上传:XMLHttpRequestUpload,...}状态:203 statusText:"非权威信息" proto:Object
JSON.parse(response.data)以及response.data.error - >两者都给出了错误.我怎样才能读取数据?
超薄框架3.
$data = array('error' => 'Name must be entered with more than one character.');
$newResponse = $response->withJson($data, 203);
return $newResponse;
Run Code Online (Sandbox Code Playgroud)
Mos*_*ini 16
在Axios中,响应已经作为javascript对象提供,无需解析,只需获取响应和访问数据.
Rob*_*eiz 13
假设来自服务器的响应如下所示:
{"token": "1234567890"}
Run Code Online (Sandbox Code Playgroud)
然后在 Axios 中,您可以像这样访问它:
console.log( response.data.token )
Run Code Online (Sandbox Code Playgroud)
这发生在我身上,因为我写了
import { Axios } from "axios"; // named export: problem
const instance = new Axios({ baseURL: "#####" }); // new Axios: problem
Run Code Online (Sandbox Code Playgroud)
当它应该是
import axios from "axios"; // default export: no problem
const instance = axios.create({ baseURL: "#####" }); // axios.create: no problem
Run Code Online (Sandbox Code Playgroud)
学分: https: //github.com/axios/axios/issues/3419#issuecomment-1015921836
正如已经写过的,axios 已经默认返回 JSON。只需使用response.data作为简单的JS对象即可。
但是,以下见解可能对其他人有帮助:我遇到了 Axios 以字符串形式返回响应的问题。经过调查,我发现服务器返回了无效的 JSON(它是静态文件服务器)。当修复 JSON 格式后,Axios 再次使用 JSON 而不是字符串。
小智 5
你可以简单地得到它,如下所示,
前任:
{
"terms": {
"title": "usage",
"message": "this is the usage message"
}
}
Run Code Online (Sandbox Code Playgroud)
当响应看起来像这样时,您可以使用“response.data”来获取它,依此类推......
.then(response =>
console.log( response.data.terms.message)
Run Code Online (Sandbox Code Playgroud)
干杯!
| 归档时间: |
|
| 查看次数: |
35770 次 |
| 最近记录: |