1、axois介绍
直接看中文文档,点击跳转
2、踩坑点
一开始以为delete方法的传递参数和put post 应该差不多,就按照这个想法写,结果搞死没有把参数带到后端去,错误写法如下:
util.ajax.delete('api/space/app_delete', qs.stringify({
id: this.id
}), {headers: {'Content-Type': 'application/x-www-form-urlencoded;charset:UTF-8'}})
.then(response => {
this.loading = false;
if (response.data.code === this.GLOBAL.requestOk) {
this.$Message.success('项目删除成功!');
} else {
this.$Message.error(response.data.list.msg);
return false;
}
}).catch(error => {
this.$Message.error(error.message);
});
结果这样怎么都没法把id 这个参数传输给后端, 最后看了下axios的源码 发现这样写不对。。。贴出源码关键部分
request(config: AxiosRequestConfig): AxiosPromise
;get(url: string, config?: AxiosRequestConfig): AxiosPromise;
delete(url: string, config?: AxiosRequestConfig): AxiosPromise;
head(url: string, config?: AxiosRequestConfig): AxiosPromise;
post(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
put(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
patch(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
3、正确做法
发现delete的传递参数方式和put、post等不一样,第一个参数为url,这个没问题,第二个参数直接传递的是一个config ,那是不是可以这样传递参数呢?
util.ajax.delete('api/space/app_delete',
{
data: qs.stringify({
id: params.row.id})
}).then(response => {
if (response.data.code === this.GLOBAL.requestOk) {
that.tableData.splice(params.index, 1)
this.$Message.success('项目删除成功');
} else {
this.$Message.error(response.data.list);
}
this.$Modal.remove();
}).catch(error => {
this.$Message.error(error.message);
});
最后发现,上面的这样写可以将id这个参数传递给后端,这个坑了我将近1个小时,终于解决,做下记录