var service = axios.create({
baseURL: "",
withCredentials: true,
timeout: 1000 * 60 * 5
})
1、get请求传递参数,直接在url上拼接参数
//js
service({
url: $GV.ROOT+"/test/users?id=33333333",
method: "get",
data: {}
}).then(function (res) {
console.log(res)
}).catch(function (error) {
console.log(error)
})
//java
@RequestMapping(value="/users",method=RequestMethod.GET)
@ResponseBody
public String getUser(Integer id){
System.out.println("get"+id);
return "";
}
2、post请求传递参数,直接在data参数上写携带的数据。post请求默认的header是application/json是json数据格式发送,所以spring接受参数要使用一个对象来接收数据,要使用注解@RequestBody声明接收的是一个json数据对象。
//js
service({
url : $GV.ROOT + "/test/users/object",
method : "post",
data : {"id":333333333,"username":"username999999"}
}).then(function(res) {
console.log(res)
}).catch(function (error) {
console.log(error)
})
//java
@RequestMapping(value="/users/object",method=RequestMethod.POST)
@ResponseBody
public String postUser(@RequestBody TbStudent student){
System.out.println("postObject---id"+student.getId());
System.out.println("postObject---username"+student.getUsername());
return "";
}
3、post请求传递参数,直接在data参数上手动拼接所需要传递的参数,像get方法拼接参数一样拼接就可以。此时post请求默认的header是Content-Type:application/x-www-form-urlencoded发送,所以spring接受参数直接根据所拼接的参数名来接收就可以。
//js
service({
url : $GV.ROOT + "/test/users",
method : "post",
data : "id=33333333&username=username9999999999"
}).then(function(res) {
console.log(res)
}).catch(function (error) {
console.log(error)
})
//java
@RequestMapping(value="/users",method=RequestMethod.POST)
@ResponseBody
public String postUser(String id,String username){
System.out.println("postObject---id"+id);
System.out.println("postObject---username"+username);
return "";
}
4、post请求传递参数,直接在data参数上手动拼接所需要传递的参数,像get方法拼接参数一样拼接就可以。此时post请求默认的header是Content-Type:application/x-www-form-urlencoded发送,所以spring接受参数直接根据所拼接的参数名来接收就可以。这个是第三种方法的增强版,不用自己手动拼接,直接使用函数手动拼接参数,自己只需要手动书写参数对象传入方法就可以得到对应的结果。方法只对于一个对象进行解析,同时也没有对于传入对象的合法性和格式进行校验,所以对于方法的使用者要了解传入什么样的类型的参数可以得到正确的数据。
//js
var paramsObject = {
"id" : "99999999",
"username" : "99999username99999999"
};
service({
url : $GV.ROOT + "/test/users",
method : "post",
data : makeUpURIParams(paramsObject)
}).then(function(res) {
console.log(res)
}).catch(function (error) {
console.log(error)
})
//js调用的方法
function makeUpURIParams(obj) {
var _URIParams = "";
if (obj) {
Object.keys(obj).map(function(key) {
if (_URIParams && _URIParams.length > 0) {
_URIParams += "&";
}
_URIParams += key + '=' + obj[key];
})
}
return _URIParams;
}
//java
@RequestMapping(value="/users",method=RequestMethod.POST)
@ResponseBody
public String postUser(String id,String username){
System.out.println("postObject---id"+id);
System.out.println("postObject---username"+username);
return "";
}
同样的,这种方法在spring的接收参数也可以使用对象来接收。但无需添加@RequestBody注解。
//java
@RequestMapping(value="/users",method=RequestMethod.POST)
@ResponseBody
public String postUsers(TbStudent student){
System.out.println("postObject---id"+student.getId());
System.out.println("postObject---username"+student.getUsername());
return "";
}