2 回答

TA贡献1828条经验 获得超3个赞
这是因为您从未告诉过axios
发送POST
请求。使用自定义配置axios.create
创建一个新实例。axios
该实例有不同的方法(如.get()
、.post()
等),但没有一个方法是then()
,所以这就是您收到错误的原因.then is not a function
。您将默认方法设置为POST
,但从未发送过请求。
我认为您想要创建这个新实例,因为您不想每次都添加基本 URL 和标头。如果要创建基础实例,可以将返回值分配给新变量:
const API = axios.create({
baseURL: 'http://localhost:8000/api/',
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
});
并使用此实例来发布您的请求:
API.post('store', data)
.then(res => {
console.log(res);
console.log(res.data);
});

TA贡献1868条经验 获得超4个赞
你可以尝试使用这个语法发帖吗?
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);
});
添加回答
举报