Question: How to Read Respose form a WebAPI in ReactJS

Question

How to Read Respose form a WebAPI in ReactJS

Answers 1
Added at 2017-01-03 08:01
Tags
Question

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

Answers
nr: #1 dodano: 2017-01-03 08:01

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)
    })
Source Show
◀ Wstecz