【发布时间】:2018-10-09 20:42:37
【问题描述】:
const URL = "http://url.com";
fetch(URL).then(res => res.json()).then(json => {
this.setState({ someData: json });
});
如何发送带有 HTTP 标头的 HTTP 请求?
【问题讨论】:
标签: javascript reactjs
const URL = "http://url.com";
fetch(URL).then(res => res.json()).then(json => {
this.setState({ someData: json });
});
如何发送带有 HTTP 标头的 HTTP 请求?
【问题讨论】:
标签: javascript reactjs
试试这个
fetch('your_url', {
method: 'get',
headers: new Headers({
// Your header content
})
});
【讨论】:
new Headers?为什么不直接使用headers: { ... }?
您可以将它们传递给fetch():
const API = 'foo';
fetch(API, { headers: {
'user-agent': 'Mozilla/4.0 MDN Example',
'content-type': 'application/json'
}}).then()
您可以阅读更多关于 here 的信息。
【讨论】:
在fetch() 方法中你应该做这样的事情
fetch(url, {
...
headers: {
'user-agent': 'Mozilla/4.0 MDN Example',
'content-type': 'application/json'
}
更多详情,请查看Mozilla Developers documentation。
【讨论】: