如何阅读Resact在ReactJS中形成一个WebAPI

时间:2021-04-01 21:19:31

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

我无法在ReactJS代码中读取响应,但API调用返回正确的JSON数据,响应代码为:200

My ReactJS code is

我的ReactJS代码是

 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

Web API将以下JSON返回给浏览器

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

Kindly help me to read the following JSON using ReactJS

请帮助我使用ReactJS阅读以下JSON

Response of Answer : @OB3:

回答:@ OB3:

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

我试过了同样的,但我得到了例外。请参阅截图并帮助我。

如何阅读Resact在ReactJS中形成一个WebAPI

1 个解决方案

#1


0  

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)
    })

#1


0  

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)
    })