How to Read Respose form a WebAPI in ReactJS

557 Views Asked by At

I'm not able to read the Response in the ReactJS code, but the API Call is returning the proper JSON data with Response Code: 200

My ReactJS code is

 fetch('http://localhost:8090/api/Contact/GetContactType', {mode: 'no-cors'})
            .then(response=> {
                console.log(response.data);
                console.log(response);

            });

The Web API is returning the following JSON to the browser

[
   {
      "ContactTypeId":1,
      "ContactType":"Seller"
   },
   {
      "ContactTypeId":2,
      "ContactType":"Re-Seller"
   }
]

Kindly help me to read the following JSON using ReactJS

Response of Answer : @OB3:

I tried the same, but I'm getting the Exception. Kindly refer the Screenshot and help me.

enter image description here

1

There are 1 best solutions below

4
On

You would have to parse the response body first.

fetch('http://localhost:8090/api/Contact/GetContactType', {mode: 'no-cors'})
    .then(function(response) {
        return response.json()
    }).then(function(json) {
        console.log('parsed json', json)
    }).catch(function(ex) {
        console.log('parsing failed', ex)
    })