首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用redux和rails api处理post请求

使用redux和rails api处理post请求
EN

Stack Overflow用户
提问于 2020-12-24 18:56:21
回答 1查看 19关注 0票数 0

我正在尝试在api中使用fetch medthod发布一些数据

代码语言:javascript
运行
复制
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只得到响应,而不是创建的数据

代码语言:javascript
运行
复制
Response {type: "basic", url: "http://localhost:3000/api/matches", redirected: false, status: 200, ok: true, …}

我不知道如何创建数据。

在rails中,这就是我所拥有的,我没有任何匹配的数据,只有id和时间戳

代码语言:javascript
运行
复制
def create
  @match = Match.new
  authorize @match
  if @match.save
    render json: @match
  else
    render_error
  end
end
EN

回答 1

Stack Overflow用户

发布于 2020-12-24 19:02:19

我刚刚找到了一个带有异步/等待功能的答案

代码语言:javascript
运行
复制
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
 }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65437345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档