TestController.java
package com.fengqing.vue.controller;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class TestController {
@RequestMapping("/hello")
public String hello(String name, String gender){
System.out.println("name:" + name);
System.out.println("gender:" + gender);
Map<String, String> map = new HashMap<>();
map.put("name", "jack");
return JSONObject.toJSONString(map);
}
}
test01.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<title>发送get请求</title>
</head>
<body>
<div id="app">
<div>数据:{{msg}}</div>
<button @click="sendGetByStr">发送get请求</button>
</div>
<script>
new Vue({
el:"#app",
data:{
msg:null
},
methods:{
sendGetByStr:function () {
var that = this;//关键
//传递参数
axios.get("/hello",{
params:{
name:"tom",
gender:"male"
}
})
.then(function (response) {
that.msg = response.data.name;
})
.catch(function (error) {
console.log(error);
})
}
}
});
</script>
</body>
</html>