[英]How to get data in post axios response php next.js?
this is the axios post response I tried to get the text of error in the response of the post axios I start with (Res.request.response) It give object in object in object When I went to add .data
its became undefined
.这是 axios 帖子响应我试图在我开始的帖子 axios 的响应中获取错误文本 (Res.request.response) 它在对象中的对象中给出对象 当我去添加
.data
它变成了undefined
。
{data: {…}, status: 200, statusText: "OK", headers: {…}, config: {…}, …}
config: {url: "https/v1/user/login", method: "post", data: "{"email":"09@gmail.com","password":"it"}", headers: {…}, transformRequest: Array(1), …}
data: {success: true, statusCode: 200, data: {…}}
headers: {access-control-allow-headers: "x-requested-with, Content-Type, origin, authorization, accept, client-security-token", access-control-allow-methods: "POST, GET, OPTIONS, DELETE, PUT", access-control-allow-origin: "*", access-control-max-age: "1000", connection: "keep-alive", …}
request: XMLHttpRequest
onabort: ƒ ()
onerror: ƒ ()
onload: null
onloadend: null
onloadstart: null
onprogress: null
onreadystatechange: ƒ ()
ontimeout: ƒ ()
readyState: 4
response: "{"success":true,"statusCode":200,"data":{"errors":{"password":["Incorrect email or password"]}}}"
responseText: "{"success":true,"statusCode":200,"data":{"errors":{"password":["Incorrect email or password"]}}}"
responseType: ""
responseURL: "https://cors-anywhere.herokuapp.com/https://api.wodworx.com/v1/user/login"
responseXML: null
status: 200
statusText: "OK"
timeout: 0
upload: XMLHttpRequestUpload {onloadstart: null, onprogress: null, onabort: null, onerror: null, onload: null, …}
withCredentials: false
__proto__: XMLHttpRequest
status: 200
statusText: "OK"
when I try console.log(Res.request.response) this is the result当我尝试 console.log(Res.request.response) 这是结果
{"success":true,"statusCode":200,"data":{"errors":{"password":["Incorrect email or password"]}}}
How can I iterate to get this result: Incorrect email or password
如何迭代以获得此结果:
Incorrect email or password
Pretty sure you are skipping a data
here.很确定你在这里跳过了一个
data
。
Try Res.request.response.data.data.errors.password.join('\\n')
试试
Res.request.response.data.data.errors.password.join('\\n')
That .join('\\n') is optional. .join('\\n') 是可选的。 You can replace it with
...password[0]
.您可以将其替换为
...password[0]
。
If you'd like to learn a bit, this is how I approach exploring this object.如果你想学习一点,这就是我探索这个对象的方法。 It's kind of a skillset in and of itself.
这本身就是一种技能。
Let's give it a shot.让我们试一试。
From what you printed above ( Res.request.response
), I did the following:根据您上面打印的内容(
Res.request.response
),我执行了以下操作:
let respData = JSON.parse('{"success":true,"statusCode":200,"data":{"errors":{"password":["Incorrect email or password"]}}}');
console.log(respData.data.errors.password.join('\n'));
// logs: "Incorrect email or password"
That worked.那奏效了。
Notice, console.log surrounds the data with quotes indicating it's type string.注意,console.log 用引号将数据括起来,表明它是字符串类型。
Looking at Res.request.data
, there are no quotes around it.查看
Res.request.data
,它周围没有引号。 It looks like an object.它看起来像一个物体。 Guessing axios already called JSON.parse on the value.
猜测 axios 已经在值上调用了 JSON.parse。
Since Res.request.response
and Res.request.data
are at the same level in the object, it would appear that Res.request.data === {success: true, statusCode: 200, data: {…}}
.由于
Res.request.response
和Res.request.data
在对象中处于同一级别,看起来Res.request.data === {success: true, statusCode: 200, data: {…}}
。
To get at the data you're after, you'd need to call Res.request.data.data
to access that part of the response.要获得您想要的数据,您需要调用
Res.request.data.data
来访问响应的那部分。
In other words:换句话说:
let responseData = Res.request.data;
responseData.success === true
let errors = responseData.data.errors
let firstPasswordError = errors.password[0]
If that doesn't help, let me know.如果这没有帮助,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.