【问题标题】:what is the right way to POST an auth form using isomorphic-fetch?使用同构获取发布身份验证表单的正确方法是什么?
【发布时间】:2016-10-09 03:51:57
【问题描述】:

我正在使用以下代码从调用 express POST 端点的 react/redux 应用发送 ajax 请求。
我很确定问题不在服务器端,因为我已经用 Postman 测试了这个端点并且它工作正常。 所以这是我在客户端使用的代码,它从护照身份验证返回{message: "Missing credentials"}...再次,服务器上没有问题...我正确使用bodyparser,注销req.body.email和@987654323 @ - 它们使用 Postman 正确打印,但使用 isomorphic-fetch 和以下代码启动此调用会在服务器上打印以下 req.body...

{ '------WebKitFormBoundaryKtn3SHumfq4YBeZ1\r\nContent-Disposition: form-data; name': '"email"\r\n\r\ntest@domain.com\r\n------WebKitFormBoundaryKtn3SHumfq4YBeZ1\r\nContent-Disposition: form-data; name="password"\r\n\r\ntestPassword\r\n------WebKitFormBoundaryKtn3SHumfq4YBeZ1--\r\n' }


import fetch    from 'isomorphic-fetch'
import FormData from 'form-data'

export function signup(payload) {

    return (dispatch) => {

        let formData = new FormData()
        formData.append('email',payload.email)
        formData.append('password',payload.password)

        fetch(`${baseURL}/signup`, {
          method: 'post',
          body: formData,
          headers: new Headers({
            'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
            'Accept': 'application/json, application/xml, text/plain, text/html, *.*'
          }),
        })
        .then((res) => res.json())
        .then((res) => {
            console.log('Fetch signup result:',res)
            console.log('dispatch:',dispatch)
            dispatch({
                type        : Auth_Actions.SignUp_Success,
                userObject  : res
            })
        })
        .catch((err)=>{
            console.error('Fetch signup ERROR:',err)
            dispatch({
                type        : Auth_Actions.SignUp_Fail,
                userObject  : err
            })
        });

    }
}

【问题讨论】:

    标签: express reactjs fetch redux isomorphic-fetch-api


    【解决方案1】:

    您是否尝试添加您的授权令牌?标题中的类似内容:

      headers: {
        'Accept': 'application/json',
        'Authorization': token.id,
        'Content-Type': 'application/json'
      }
    

    【讨论】:

      猜你喜欢
      • 2019-04-23
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 2021-07-04
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多