欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue中使用$http.post請求傳參的錯誤及解決

 更新時間:2022年04月08日 08:48:00   作者:B–rabbit  
這篇文章主要介紹了vue中使用$http.post請求傳參的錯誤及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

使用$http.post請求傳參的錯誤

在使用$http請求后臺,照常我們在后端 使用注解@PostMapper或者 @RequestMapping(value = “XXXX”,method = RequestMethod.POST)接受請求

	 handleAdd(node) {
		this.$http.post("/item/category/addCategory",{
				node:node
				})
				.then(({data})=>{
						this.$emit("close");
						this.$message.success("添加成功!");
				}).catch(() => {
				     this.$message.error("添加失敗!");
				    });
				    }

結(jié)果報了一個’GET’的錯誤,我就很納悶。并沒有寫get方法,那必定會出錯。

在這里插入圖片描述

截圖奉上==

在這里插入圖片描述

傳遞的是json對象而不是數(shù)組。

還好之前有過post傳遞參數(shù)的項目之后又搜了一下,

如下面這種

 handleAdd(node) {
		this.$http({
					method:'post',
					url:'/item/category/addCategory',
					data: node
				}).then(({data})=>{
						this.$emit("close");
						this.$message.success("添加成功!");
				}).catch(() => {
             this.$message.error("添加失??!");
            });
            }

整體上沒有問題

在這里插入圖片描述

之后我有嘗試了一下

 handleAdd(node) {
	this.$http.post("/item/category/addCategory",{
				node:this.$qs.stringify(node)
				})
				.then(({data})=>{
						this.$emit("close");
						this.$message.success("添加成功!");
				}).catch(() => {
				     this.$message.error("添加失敗!");
				    });
	}

傳遞的結(jié)果

在這里插入圖片描述

三種方式都可以使用,但我只有第二種方式成功啦。

vue post請求之坑

最近用的vue請求數(shù)據(jù),坑死,還是對前端vue框架不熟。

與后端通信有問題,要么是json加入到url有問題、要么是json解析有問題。

解決方法

1、請求參數(shù)一個用url傳

?var json=[{"msg”:“123"}];
?var temp=encodeURI(JSON.stringify(json)); ? ? ?//重點
?var urls="http://202.114.207.232:8080/web/data?operations="+temp;
? ? ? ? ? ? this.$axios({type:'post',url:urls, dataType:'json' }).then( res => { console.log(res) }).catch( e => { console.info(e) })

2、一個用data包裝傳

var Data=[{}]
var url = "http://111.111.111.111:8080/web/data";
var params = new URLSearchParams();
params.append("operations", JSON.stringify(Data)); ? ? ? ? ? //重點
// params.append('param2', 'value2');
that.$axios.post(url, params)
? ? .then(response => {
? ? ? ? // post 成功,response.data 為返回的數(shù)據(jù)
? ? ? ? console.log(response.data)
? ? })
? ? .catch(error => {
? ? ? ? // 請求失敗
? ? ? ? console.log(error)
? ? })

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。 

相關(guān)文章

最新評論