我有一个 React JS 应用程序,它正在尝试登录基于 token 的 API(我也在开发,使用 Laravel 5.5 和 Laravel Passport)。我正在尝试实现基本登录(使用用户名和密码登录,验证后从服务器接收 token ,使用该 token 进行将来的查询)。
我用来获取的代码如下:
function loginApi(username, password) {
return fetch(loginUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
grant_type: 'password',
client_id: clientId,
client_secret: clientSecret,
username: username,
password: password
})
}).then(handleApiErrors)
.then(response => response.json)
}
值得注意的是,它并不完全完成,因为我目前只是试图确定我得到的响应。
我已经解决了人们遇到的常见问题,例如 CORS 问题,但是我现在遇到的问题是 API 的响应只是......空。即使在 Chrome 开发者工具的“网络”选项卡中,如果我直接检查请求,响应也是空的。
但是,如果我使用 Postman 进行完全相同的相同查询,结果正是我所期望的。
这里可能出现什么问题?为什么完全相同的查询会返回不同的结果?
最佳答案
.json 是一个函数,需要调用。尝试一下..
.then(response => response.json())
您当前正在返回 .json 解析函数。
关于laravel - React JS Fetch 返回空响应,但 Postman 不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46164068/