【发布时间】:2018-11-27 19:52:50
【问题描述】:
我正在搞乱 react 并尝试向 api 发出 put 请求。我正在尝试将我的正文作为内容类型应用程序/json 发送,但它一直以文本/纯文本形式发送。如果我添加标题 "Content-Type" : "application/json" 我只是得到这个作为回报。
预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 Content-Type。
我正在使用这个 API:http://funtranslations.com/api/yoda
这是我的要求:
fetch('http://api.funtranslations.com/translate/yoda.json', {
method: 'PUT',
body: JSON.stringify({
text: this.state.input
})
})
.then(res => {
return console.log(res.json());
})
}
提前感谢您尝试帮助我:)
【问题讨论】:
-
@Xarvalus 我正在使用这个公共 API funtranslations.com/api/yoda,因此,我无法更改响应标头。有什么方法可以在我的请求中不包含“Content-Type”标头的情况下将我的 JSON 字符串作为 application/json 发送?
-
Content-Type 完全没问题,如果你真的在处理 JSON 对象,它应该是这样的。您有一些可能忽略 CORS 问题,请看这里:“Cors issue with third party api”
标签: javascript reactjs fetch-api