web端
- 下载axios
npm install axios
- main.js中引入 axios
import axios from 'axios'
axios.defaults.baseURL='http://127.0.0.1:9999/demo'//配置请求地址。
Vue.prototype.$http=axios;//修改内部的$http为axios
- 发送post请求
this.$http.post(url,param)
.then(res=>{
console.log(res.data);
})
.catch(error=>{
console.log(error)
})
服务端
@PostMapping("/***")
public ReturnParam findall(String id){
······
return returnPatam;
}
问题1:
页面控制台报错:Failed to execute 'open' on 'XMLHttpRequest': Invalid URL
解决思路:
检查你的请求地址是否添加http://
问题2:
页面求情没有报错,但是后台收不到返回值。
解决思路:
① this.$http.post(url,param)
中 param由json改为字符串形式
{id:"1"} => "id=1"
如果是多个值
{"id":"1","name":"2"} => "id=1&name=2"
②使用 @RequestBody
在接参数前加上@RequestBody的注解
@PostMapping("/***")
public ReturnParam findall(@RequestBody User user){//这里必须是个对象来接请求值
······
return returnPatam;
}
后就可以直接发送json了
问题3:
跨域问题。
这个之后受时间补齐