Why react native fetch always return 200 even if login failed?

Viewed 1732

I use these code to post data to login

postJson(url, data, callback){
        var fetchOptions = {
          method: 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          },
          body:data
        };
        console.log('begin request');
        fetch(url, fetchOptions)
        .then((response) => {
          responseJson = response.json()
          console.log(responseJson, response.status)
          callback(responseJson)
        })
        .catch((error) => {
          console.error(error);
        });
  }

But quite strange when I use it as

postJson(url, formData, (responseJson) => {
           if (responseJson == 200){
              console.log(responseText)
              this.onLoginSuccess();
           }
           else{
              alert('fail');
           }
    })

The response code is always 200, but login failed. I have tested the api with postman, it works fine.enter image description here

actually what console.log(responseJson, response.status) got is

Promise {
  "_40": 0,
  "_55": null,
  "_65": 0,
  "_72": null,
} 200

Update: this problem is caused by the basic auth in server side. Just add encoding can fix this. Thanks

2 Answers
Related