我有一个反应/终极版的应用程序,我试图做一个简单的GET请求到服务器:反应过来取给出了一个空的响应体
fetch('http://example.com/api/node', {
mode: "no-cors",
method: "GET",
headers: {
"Accept": "application/json"
}
}).then((response) => {
console.log(response.body); // null
return dispatch({
type: "GET_CALL",
response: response
});
})
.catch(error => { console.log('request failed', error); });
的问题是,响应主体是空的.then()
功能,我不知道为什么。我在网上查了一些例子,它看起来像我的代码应该工作,所以我显然在这里失去了一些东西 问题是,如果我在Chrome的开发工具中查看网络选项卡,则发出请求,并收到我正在查找的数据。
任何人都可以照亮这一个吗?
编辑:
我试图转换效应初探。
使用.text()
:
fetch('http://example.com/api/node', {
mode: "no-cors",
method: "GET",
headers: {
"Accept": "application/json"
}
})
.then(response => response.text())
.then((response) => {
console.log(response); // returns empty string
return dispatch({
type: "GET_CALL",
response: response
});
})
.catch(error => { console.log('request failed', error); });
与.json()
:
fetch('http://example.com/api/node', {
mode: "no-cors",
method: "GET",
headers: {
"Accept": "application/json"
}
})
.then(response => response.json())
.then((response) => {
console.log(response.body);
return dispatch({
type: "GET_CALL",
response: response.body
});
})
.catch(error => { console.log('request failed', error); }); // Syntax error: unexpected end of input
寻找在Chrome浏览器开发工具:
我无法使'fetch'工作,所以我开始使用[redux-api-middleware](https://github.com/agraboso/redux-api-middleware/)。当你提出请求时,你必须以类似的方式将响应转换成json: –