【发布时间】:2018-09-21 03:03:22
【问题描述】:
我无法通过 axios 设置标题,例如内容类型为应用程序/json。 axios 没有抛出异常,但请求在浏览器中得到“415 Unsupported Media Type”响应。有什么线索我错了吗?
以下是代码-
我尝试了两种方法。
方法 1 -
import axios from 'axios';
axios.defaults.headers.common['Content-Type'] = 'application/json';
axios.get(url).then(
response => this.loadData(response)).catch(
(error) => this.showError(error));
方法 2 -
let config = {
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json"
}
};
axios.get(url, config).then(
response => this.loadData(response)).catch(
(error) => this.showError(error));
【问题讨论】:
-
不需要设置content-type application/json,它会自动设置,另一个header Access-Control-Allow-Origin 需要允许从你的后端服务器(快递)而不是你的react app.try 删除两个标题
-
我不认为你是sending json。
标签: reactjs ecmascript-6 axios