使用Fetch APi时如何设置请求标头的内容类型 [英] How to set the content-type of request header when using Fetch APi
本文介绍了使用Fetch APi时如何设置请求标头的内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用npm'isomorphic-fetch'发送请求.我遇到的问题是我无法设置请求标头的内容类型.
I am using npm 'isomorphic-fetch' to send requests. The problem I am experiencing is I am unable to set the content-type of the request header.
我将内容类型设置为application/json,但是请求标头已设置为text/plain.
I set a content type of application/json , however the request header are being set to text/plain.
import 'isomorphic-fetch';
sendRequest(url, method, body) {
const options = {
method: method,
headers:{'content-type': 'application/json'},
mode: 'no-cors'
};
options.body = JSON.stringify(body);
return fetch(url, options);
}
当我在浏览器中检查请求时,内容类型为o:
When I examine the request in my browser the content type is o :
content-type:text/plain;charset=UTF-8
任何人都可以解释为什么我无法设置此属性吗?
Can anyone explain why I am unable to set this property?
推荐答案
您需要创建访存标头对象.
You need to create a fetch headers object.
sendRequest(url, method, body) {
const options = {
method: method,
headers: new Headers({'content-type': 'application/json'}),
mode: 'no-cors'
};
options.body = JSON.stringify(body);
return fetch(url, options);
}
这篇关于使用Fetch APi时如何设置请求标头的内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文