API Rails 上的获取请求不返回 json [英] Fetch request on API Rails doesn't return json
问题描述
在 Rails API 中,我的 UsersController 中有一个登录 POST 方法,该方法接受 2 个参数(邮件和密码),如果找到记录,则检查 DB,如果找到,则将其返回为 JSON.
In a Rails API, I have a login POST method in my UsersController which takes 2 parameters (mail and password) and check in DB if a record is found and if so returns it as JSON.
def login(mail, password)
mail, password = params.values_at(:mail, :password)
user = User.where(mail: mail, password: password)
render json: user
end
在我的前端,在 React 中,我使用 fetch 调用此方法,它以表单形式获取邮件和密码值,并希望在我的 'res'
中将用户作为 JSON:
In my front side, in React, I call this method with fetch which takes the mail and password values in a form and expect to have the user as JSON in my 'res'
:
login = () => {
if(this.state.mail != null && this.state.password != null){
fetch('http://127.0.0.1:3001/api/login', {
method: 'post',
body: JSON.stringify({
mail: this.state.mail,
password: this.state.password
}),
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
}
})
.then((res) => {
console.log(res)
if(res.data.length === 1 ){
const cookies = new Cookies();
cookies.set('mercato-cookie',res.data[0].id,{path: '/'});
this.setState({redirect: true})
}
})
} bodyUsed: false
headers: Headers { }
ok: true
redirected: false
status: 200
statusText: "OK"
type: "cors"
url: "http://127.0.0.1:3001/api/login"
__proto__: ResponsePrototype { clone: clone(), arrayBuffer: arrayBuffer(), blob: blob(), … } auth.js:32
}
问题是我的 res
与我返回的 render json: user
不对应,所以我做了一个 console.log(res)代码>:
Problem is my res
doesn't correspond to what I return with render json: user
, so I made a console.log(res)
:
Response
bodyUsed: false
headers: Headers { }
ok: true
redirected: false
status: 200
statusText: "OK"
type: "cors"
url: "http://127.0.0.1:3001/api/login"
__proto__: ResponsePrototype { clone: clone(), arrayBuffer: arrayBuffer(), blob: blob(), … } auth.js:32
我尝试返回简单的 JSON 文本以防我的 user
变量出现问题,还尝试将 render json: user
更改为 format.json { renderjson: 用户 }
但没有结果:/
I tried returning simple JSON text in case there was a problem with my user
variable and also tried changing render json: user
to format.json { render json: user }
but with no result :/
我在 Postman 上提出了请求,它返回了适当的 JSON,所以我猜问题出在我的 fetch
上?
I made the request on Postman and it returns the appropiate JSON, so i guess the problem comes from my fetch
?
推荐答案
Fetch 的响应不会自动转换成 JSON,你需要调用 response.json()
(返回一个 promise)为了获取 JSON 值.请参阅 这个来自 MDN 的示例,或者这里有一些 ES6 来匹配你的代码:
Fetch's response doesn't automatically translate to JSON, you need to call response.json()
(which returns a promise) in order to get the JSON value. See this example from MDN, or here's some ES6 to match your code:
fetch(myRequest)
.then(response => response.json())
.then((data) => {
// I'm assuming you'll have direct access to data instead of res.data here,
// depending on how your API is structured
if (data.length === 1) {
const cookies = new Cookies();
cookies.set('mercato-cookie', data[0].id, {path: '/'});
this.setState({redirect: true});
}
});
这篇关于API Rails 上的获取请求不返回 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!