fetch反应原生返回401错误

时间:2017-01-12 17:11:07

标签: nginx proxy react-native fetch

所以我能够通过postman成功发送请求,但每当我把它扔进fetch时,我都会收到401错误。

export const createUser = () => {
 return async (dispatch) => {
 dispatch({ type: CREATE_USER });

console.log('we are in the create user function');

try {
  let response = await fetch('secret.com/v1/login/signup', {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      email: 'test1231273123@test.com',
      password: 'Asadasd123123',
      first_name: 'joe',
      last_name: 'doe',
      phone_number: '373738'
    })
  });
  console.log('response ' + JSON.stringify(response));
} catch (error) {
  console.log(error);
}
  };
};

这是我一直收到的错误。 response {“type”:“default”,“status”:401,“ok”:false,“headers”:{“map”:{“access-control-allow-methods”:[“GET,POST,PUT, PATCH,DELETE,HEAD,OPTIONS“],”access-control-allow-origin“:[”*“],”connection“:[”keep-alive“],”access-control-allow-credentials“:[” true“],”content-length“:[”188“],”content-type“:[”text / html; charset = utf-8“],”access-control-allow-headers“:[”Content-键入,接受,授权“],”www-authenticate“:[”Basic realm = \“Restricted \”“],”date“:[”星期四,2017年1月12日16:57:58 GMT“],”服务器“ :[ “nginx的”]}}, “URL”: “https://secret.com/v1/login/signup”, “_ bodyInit”:{}, “_ bodyBlob”:{}}

我的后端开发人员认为我遇到了跨域问题,需要设置代理服务器吗? “设置一些代理服务器(我建议使用nginx),它会将ajax查询代理到我们的API域”

我认为它与fetch有关?想法?

1 个答案:

答案 0 :(得分:1)

我相信你需要提供协议,改变:

await fetch('secret.com/v1/login/signup'...

await fetch('http://secret.com/v1/login/signup'