我正在尝试在api中使用fetch medthod发布一些数据
export const CREATE_MATCH = 'CREATE_MATCH'
export function createMatch(user) {
const request = fetch("/api/matches", {
// Adding method type
method: "POST",
// Adding headers to the request
headers: {
"Content-type": "application/json",
"X-User-Token": user.authentication_token,
"X-User-Email": user.email
}
})
return {
type: CREATE_MATCH,
payload: request
}
}
但是I只得到响应,而不是创建的数据
Response {type: "basic", url: "http://localhost:3000/api/matches", redirected: false, status: 200, ok: true, …}
我不知道如何创建数据。
在rails中,这就是我所拥有的,我没有任何匹配的数据,只有id和时间戳
def create
@match = Match.new
authorize @match
if @match.save
render json: @match
else
render_error
end
end
发布于 2020-12-24 19:02:19
我刚刚找到了一个带有异步/等待功能的答案
export async function createMatch(user) {
const request = await fetch("/api/matches", {
// Adding method type
method: "POST",
// Adding body or contents to send
// body: JSON.stringify(),
// Adding headers to the request
headers: {
"Content-type": "application/json",
"X-User-Token": user.authentication_token,
"X-User-Email": user.email
}
})
const match = await request.json();
console.log(match)
return {
type: CREATE_MATCH,
payload: match
}
}
https://stackoverflow.com/questions/65437345
复制相似问题