0
fetch(loginApi, {
  method: "POST",
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    Password: password,
    UserName: username
  })
})
  .then(response => response.json())
  .then(responseData => {
    console.log(responseData); //// <--- getting error at this line
  })
  .catch(error => {
    console.error(error);
  });

enter image description here

Any idea how to solve it??? Thanks in advance.

2 Answers2

0

maybe response data is not in json format try this

fetch(loginApi, {
  method: "POST",
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    Password: password,
    UserName: username
  })
}).then((e)=>{
 console.log(e) //see what you get, if promise then use another then 
})
Jigar
  • 1,314
  • 9
  • 19
  • not promise right? it must promise to use .then(response => response.json()) – Jigar Dec 07 '17 at 08:03
  • response data is a JSON. but still i did what you have asked. and got something like this [Info] I/ReactNativeJS(25223): { type: 'default', I/ReactNativeJS(25223):status: 500, I/ReactNativeJS(25223):ok: false, I/ReactNativeJS(25223): statusText: undefined, I/ReactNativeJS(25223): headers: I/ReactNativeJS(25223): { map: I/ReactNativeJS(25223): { 'content-length': [ '1849' ], I/ReactNativeJS(25223): date: [ 'Thu, 07 Dec 2017 08:00:02 GMT' ], – Saddaf Afrin Khan Dec 07 '17 at 08:12
  • that means there is something wrong in response JSON – Jigar Dec 07 '17 at 08:14
0

Luckily I have found the solution.

var loginParams = {
        Password: password,
        UserName: username
    };

    var loginFormData = new FormData();

    for (var param in loginParams) {
        loginFormData.append(param, loginParams[param]);
    }

    fetch(loginApi, {
     method: "POST",
      headers: { /// <---- you can remove this headers if you are not using any headers in postman..
    Accept: "application/json",
    "Content-Type": "application/json"
  },
  body: loginFormData
})
  .then(response => response.json())
  .then(responseData => {
    console.log(responseData); //// <--- getting error at this line
  })
  .catch(error => {
    console.error(error);
  });

for more information please check this enter link description here