AXIOS获取请求失败400 React Native

时间:2020-06-11 18:24:43

标签: reactjs react-native get request axios

我正在尝试向此端点发出axios获取请求,但我一直收到此错误“ [错误:请求失败,状态码为400]”。

 clikk = () => {
    console.log('saasdasdl');
    var user = 'reflect-user';
    var pass = 'user1Pass';
    let dta = JSON.stringify({
      username: 'test.admin',
      password: 'password',
      emailAddress: 'test@gmai.com',
    });
    const headers = {
      'Content-Type': 'application/json',
      Authorization: 'Basic ctesmtVmbGVjdC11c2VyOnVzZXIxUGFzcw==',
      'Access-Control-Allow-Origin': '*',
      accept: 'application/json',
    };

    // var bytes = utf8.encode(user + ':' + pass);
    // var authorizationBasic = base64.encode(bytes);
    axios({
      method: 'get',
      url: 'http://IpOfServer:8080/api/v1/user/getUser?all=true',
      headers: headers,
      data: qs.parse({
        username: 'test.admin',
        password: 'password',
        emailAddress: 'test@gmai.com',
      }),
    })
      .then((res) => {
        //const nameList = res.data;
        //this.setState({nameList});
        console.log(res);
      })
      .catch((error) => console.log(error));
  };

然而,相同的请求在POSTMAN中仍有效,因此可能不涉及API?我也尝试过POSTMAN提供的AXIOS示例,但是遇到了同样的错误。

var data = JSON.stringify({"username":"test.admin","password":"password","emailAddress":"test6@gmai.com"});

var config = {
  method: 'get',
  url: 'http://IpOfServer:8080/api/v1/user/getUser?all=true',
  headers: { 
    'Authorization': 'Basic cmVmbGVjdC11c2VyOnVzZXIxUGFzcw==', 
    'Content-Type': 'application/json'
  },
  data : data
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

谢谢。

0 个答案:

没有答案