我有一个react / redux应用程序,我正在尝试向服务器发出一个简单的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 dev工具:
答案 0 :(得分:91)
我刚碰到这个。正如本answer中所述,使用mode: "no-cors"
将为您提供opaque response
,它似乎不会返回正文中的数据。
opaque:对跨域资源的“no-cors”请求的响应。 Severely restricted
就我而言,我使用的是Express
。安装cors for Express并配置并删除mode: "no-cors"
后,我收到了一份承诺。响应数据将在承诺中,例如
fetch('http://example.com/api/node', {
// mode: 'no-cors',
method: 'GET',
headers: {
Accept: 'application/json',
},
},
).then(response => {
if (response.ok) {
response.json().then(json => {
console.log(json);
});
}
});
答案 1 :(得分:16)
您需要先将response
转换为json
,然后才能访问response.body
来自docs
fetch(url)
.then(response => response.json())
.then(json => {
console.log('parsed json', json) // access json.body here
})
答案 2 :(得分:5)
您必须阅读回复的正文:
fetch(url)
.then(res => res.text()) // Read the body as a string
fetch(url)
.then(res => res.json()) // Read the body as JSON payload
一旦你读完了身体,你就可以操纵它了:
fetch('http://example.com/api/node', {
mode: "no-cors",
method: "GET",
headers: {
"Accept": "application/json"
}
})
.then(response => response.json())
.then(response => {
return dispatch({
type: "GET_CALL",
response: response
});
})
答案 3 :(得分:1)
尝试使用response.json():
fetch('http://example.com/api/node', {
mode: "no-cors",
method: "GET",
headers: {
"Accept": "application/json"
}
}).then((response) => {
console.log(response.json()); // null
return dispatch({
type: "GET_CALL",
response: response.json()
});
})
.catch(error => { console.log('request failed', error); });
答案 4 :(得分:1)
fetch("http://localhost:8988/api", {
method: "GET",
headers: {
"Content-Type": "application/json"
}
})
.then((response) =>response.json());
.then((data) => {
console.log(data);
})
.catch(error => {
return error;
});
答案 5 :(得分:1)
这需要更改前端JS和从后端发送的标头。
前端
在获取选项中删除"mode":"no-cors"
。
fetch(
"http://example.com/api/docs",
{
// mode: "no-cors",
method: "GET"
}
)
.then(response => response.text())
.then(data => console.log(data))
后端
当服务器响应请求时,请包含CORS标头,以指定来自请求来源的来源。如果您不关心原点,请指定*
通配符。
原始响应应包含这样的标头。
Access-Control-Allow-Origin: *
答案 6 :(得分:0)
fetch("http://localhost:8988/api", {
//mode: "no-cors",
method: "GET",
headers: {
"Accept": "application/json"
}
})
.then(response => {
return response.json();
})
.then(data => {
return data;
})
.catch(error => {
return error;
});
这适合我。
答案 7 :(得分:0)
在许多情况下,您需要在快速节点应用程序中添加bodyParser模块。
然后在app.use(express.static('www'));
下面的app.use部分中
添加这2行
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());