axios 没有得到 redux 形式的 post 请求
axios doesn't get post request in redux-form
我有这个代码:
import axios from 'axios'
const storeDevices = values => {
axios.create({
baseURL: 'http://localhost:8000/something/store',
method: 'POST',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
data: values
});
}
export default storeDevices;
以下代码是正确的,因为它 returns 一个包含我表单中所有数据的对象
const storeDevices = values => {
console.log(values);
}
export default storeDevices;
有趣的是,如果我尝试使用 .then
我有一个错误:
axios__WEBPACK_IMPORTED_MODULE_0___default.a.create(...).then is not a
function
代码 .then
axios.create({
baseURL: 'http://localhost:8000/something/store',
method: 'POST',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
data: values
}).then(res => {
console.log(res);
console.log(res.data);
});
你能尝试 post 使用这个语法吗?
axios.post('http://localhost:8000/something/store', values, {headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},}
}).then(res => {
console.log(res);
console.log(res.data);
});
这是因为您从未告诉 axios
发送 POST
请求。 axios.create
creates a new instance of axios
with a custom config. This instance have different methods(如 .get()
、.post()
等),其中 none 是 then()
,所以这就是您收到错误 .then is not a function
。您将默认方法设置为 POST
但您从未发送过请求。
我认为您想创建这个新实例是因为您不想每次都添加基数 URL 和 headers。如果你想创建一个基础实例,你可以将返回值赋值给一个新的变量:
const API = axios.create({
baseURL: 'http://localhost:8000/api/',
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
});
并使用此实例 post 您的请求:
API.post('store', data)
.then(res => {
console.log(res);
console.log(res.data);
});
我有这个代码:
import axios from 'axios'
const storeDevices = values => {
axios.create({
baseURL: 'http://localhost:8000/something/store',
method: 'POST',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
data: values
});
}
export default storeDevices;
以下代码是正确的,因为它 returns 一个包含我表单中所有数据的对象
const storeDevices = values => {
console.log(values);
}
export default storeDevices;
有趣的是,如果我尝试使用 .then
我有一个错误:
axios__WEBPACK_IMPORTED_MODULE_0___default.a.create(...).then is not a function
代码 .then
axios.create({
baseURL: 'http://localhost:8000/something/store',
method: 'POST',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
data: values
}).then(res => {
console.log(res);
console.log(res.data);
});
你能尝试 post 使用这个语法吗?
axios.post('http://localhost:8000/something/store', values, {headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},}
}).then(res => {
console.log(res);
console.log(res.data);
});
这是因为您从未告诉 axios
发送 POST
请求。 axios.create
creates a new instance of axios
with a custom config. This instance have different methods(如 .get()
、.post()
等),其中 none 是 then()
,所以这就是您收到错误 .then is not a function
。您将默认方法设置为 POST
但您从未发送过请求。
我认为您想创建这个新实例是因为您不想每次都添加基数 URL 和 headers。如果你想创建一个基础实例,你可以将返回值赋值给一个新的变量:
const API = axios.create({
baseURL: 'http://localhost:8000/api/',
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
});
并使用此实例 post 您的请求:
API.post('store', data)
.then(res => {
console.log(res);
console.log(res.data);
});