我正在学习 ReactJS。我fetch
用于从 API 获取数据。我使用了下面的代码。
fetch('http://myurl.com/api')
.then(res => res.json())
.then(res => console.log(res));
API 正在访问状态200 ok
,但响应Nothing to Preview并且在控制台中低于错误
跨域读取阻止 (CORB) 阻止 了 MIME 类型为 application/json的跨域响应http://myurl.com/api 。有关详细信息,请参阅 https://www.chromestatus.com/feature/5629709824032768 。
我还在下面的代码中添加了标题。
fetch("http://xxx.xxx.xxx.xxx:8081/api/category/popular",{
method: 'GET',
headers: {
'Access-Control-Allow-Origin': '*',
},
})
我在 API 响应中有以下 json
{
"data":[
{
"parent_id":"5c2f74e0a4d846591b2b1a40",
"icon":"http://myurl.in:8081/default.png",
"_id":"5c2f74e8a4d846591b2b1a41",
"name":"Shop",
"modified_at":"2019-01-04T14:59:52.791Z",
"created_at":"2019-01-04T14:59:52.791Z"
},
{
"parent_id":"5c2f74e0a4d846591b2b1a40",
"icon":"http://myurl.in:8081/default.png",
"_id":"5c2f7566a4d846591b2b1a42",
"name":"Home Service",
"modified_at":"2019-01-04T15:01:58.507Z",
"created_at":"2019-01-04T15:01:58.507Z"
},
{
"parent_id":"5c2f74e0a4d846591b2b1a40",
"icon":"http://myurl.in:8081/default.png",
"_id":"5c5c2dd30d017c401ec17253",
"name":"Test",
"modified_at":"2019-02-07T13:08:35.653Z",
"created_at":"2019-02-07T13:08:35.653Z",
"__v":0
}
]
}