用Vue做表单提交时遇见了一些事:
用到的 插件:axios.min.js
代码如下:
submitClick:function(){
var url = webUrl +'/api/user-comment?userId=' + user_id + '&type=' + commment_type + '&targetId=' + commment_id;
var content = this.conmentConter;
if( content == null || content == ''){
alert('^_^" 评论不能为空 ')
}else{
axios.post(url, content, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
}
检查发现:接口没问题,要提交的参数值也没问题,就是提交不成功
后来把 “ Content-Type 改成:
'Content-Type': 'application/json;charset=utf-8'
就可以了~~~。
PS:网上说:request中发送json数据用post方式发送Content-type用application/json;charset=utf-8方式发送的话,直接用springMVC的@RequestBody标签接收后面跟实体对象就行了,spring会帮你自动拼装成对象,如果Content-type设置成application/x-www-form-urlencoded;charset=utf-8就不能用spring的东西了,只能以常规的方式获取json串