ajax往后台传值的一些方式
程序员文章站
2023-11-12 08:34:22
$('#del1').click(function () { $.ajax({ url: 'http://localhost:8089/test1', data: {a: 1, b: 2}, type: 'post', success: function (r) { console.log(r) } ......
$('#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 "";
}
上一篇: Ai怎么绘制装订打孔机的图标?