$('#del1').click(function () {
$.ajax({
url: 'http://localhost:8089/test1',
data: {a: 1, b: 2},
type: 'post',
success: function (r) {
console.log(r)
}
})
})
$('#del2').click(function () {
$.ajax({
url: 'http://localhost:8089/test2',
data: {a: 1, b: 2},
type: 'post',
success: function (r) {
console.log(r)
}
})
})
//在 jquery 的 ajax 中, contentType都是默认的值:application/x-www-form-urlencoded,这种格式的特点就是,name/value 成为一组,每组之间用 & 联接,而 name与value 则是使用 = 连接。如: wwwh.baidu.com/q?key=fdsa&lang=zh 这是get , 而 post 请求则是使用请求体,参数不在 url 中,在请求体中的参数表现形式也是: key=fdsa&lang=zh的形式。
$('#del3').click(function () {
$.ajax({
url: 'http://localhost:8089/test3',
data: {id: 1, name: 'zhangsan', sex: '男'},
type: 'post',
success: function (r) {
console.log(r)
}
})
})
$('#del4').click(function () {
$.ajax({
url: 'http://localhost:8089/test4',
data: JSON.stringify({id: 1, name: 'zhangsan', sex: '男'}),
contentType: "application/json;charset=UTF-8",
type: 'post',
success: function (r) {
console.log(r)
}
})
})
$('#del5').click(function () {
$.ajax({
url: 'http://localhost:8089/test5?a=1',
data: JSON.stringify({id: 1, name: 'zhangsan', sex: '男'}),
contentType: "application/json;charset=UTF-8",
type: 'post',
success: function (r) {
console.log(r)
}
})
})
$('#del6').click(function () {
$.ajax({
url: 'http://localhost:8089/test6',
data: {arr: [1, 2, 3, 4]},
type: 'post',
success: function (r) {
console.log(r)
}
})
})
$('#del7').click(function () {
$.ajax({
url: 'http://localhost:8089/test7',
data: JSON.stringify([1, 2, 3, 4]),
contentType: "application/json;charset=UTF-8",
type: 'post',
success: function (r) {
console.log(r)
}
})
})后台代码:
@RequestMapping("/test1")
public String get1(String a,String b){
return "";
}
@RequestMapping("/test2")
public String get2(@RequestParam("a") String a,@RequestParam("b") String b){
return "";
}
@RequestMapping("/test3")
public String get3(Person person){
return "";
}
@RequestMapping("/test4")
public String get4(@RequestBody Person person){
return "";
}
@RequestMapping("/test5")
public String get5(@RequestBody Person person,@RequestParam("a") String a){
return "";
}
@RequestMapping("/test6")
public String get6(@RequestParam("arr[]") Integer[] arr){
return "";
}
@RequestMapping("/test7")
public String get7(@RequestBody Integer[] arr){
return "";
}