【发布时间】:2018-12-25 22:22:37
【问题描述】:
我是 javascript 新手,我想获取 promiseValue 中的值,该值是在 POST 之后生成的。正在连接的 API 会带来一个 json 对象,如下所示:
{
"message": "User logged in successfully",
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1MzE4NTA2ODcsImlhdCI6MTUzMTg0ODg4NywidXNlcm5hbWUiOiJ5b3VxQGdtYWlsLmNvbSJ9.oTwcHkW64RS7ooz5_dzrmlvflI4Eg2Y39hGM0D-wZkY"
}
但在 google chrome 中,我将它作为 PromiseValue 接收,所以我的目标是获取或打印 message 和 token 值promiseValue。
下图显示了我得到的 promiseValue:
以下是目前为止的代码:
fetch('https://xxxxxx.xxxxxxxx',
{
method: 'POST',
headers: {
'Accept': 'application/json,text/plain,*/*',
'Content-type': 'application/json'
},
body: JSON.stringify({
username: username,
password: password
})
}).then((res) =>{
if(res.status=='401'){
console.log(res.status)
console.log(res.json())
alert("Sorry please, invalid username or password.");
}
else if (res.status='200')
{
console.log(res.status)
console.log(res.json())
}
})
.then((data) => {
console.log(" my data : "+data)
})
【问题讨论】:
标签: javascript google-chrome post fetch-api