ajax前端传参
总结
@RequestParam()接收前端数据的情况:
POST请求“application/x-www-form-urlencoded’”,此时传参能用JSON.stringify(),,POST请求用“application/json”是后端要用@ResquestBody注解封装实体对象接收,不能用@RequestParam()。
GET请求时用“‘application/x-www-form-urlencoded’”或“application/json”都行,不用JSON.stringify()修饰传参数据
$.ajax({
type: 'get',
url: "Http://localhost:8080/user-attr/trace",
data: { "pid": data.uid },
contentType: 'application/json',
dataType: 'json',
xhrFields: {
withCredentials: true
},
success: function (res) {
if (res.code == 200) {
//获取表单数据,并序列化
layero.find("#traceUsername").attr('value', res.data.username);
layero.find("#traceAttr").attr('value', res.data.attributes);
} else {
layer.alert(res.msg, { icon: 5 }, function (index) {
layer.close(index);
});
}
}
});
@RequestBody接收前端数据
前端用form数据或者用自定义json数据传参(无法用form表单数据的情况下,不要用FormData构造,并用JSON.stringify()修饰)
$.ajax({
type: 'post',
url: "Http://localhost:8080/user-attr/updateUser",
data: JSON.stringify({"id":data.id,"info":data.Id,"username":formObject[0].value,"type":formObject[1].value,"attributes":arr_box.toString()}),
contentType : 'application/json',
dataType : 'json',
xhrFields: {
withCredentials: true
},
success: function (res) {
if (res.code==200) {
layer.alert(res.data, {icon: 1}, function (index) {
layer.close(index);
// window.location.reload();
});
} else {
layer.alert(res.msg, {icon: 5}, function (index) {
layer.close(index);
});
}
}
});
前端传自定义json参数 post请求
这是后端用@RequestParam的情况,自定义json参数 post请求时,不能用JSON.stringify()函数转化Form或FormData作为传参数据
$.ajax({
url: "Http://localhost:8080/file/search",
type : "POST",
contentType : 'application/x-www-form-urlencoded',
dataType : 'json',
data: {"keywords" : data.field.keywords, "userId" : sessionStorage.getItem('userId')},
success: function (res) {
if (res.code==200) {
resu = res.data;
} else {
layer.alert(res.msg, {icon: 5}, function (index) {
layer.close(index);
});
}
}
});
Form表单Post 请求
$.ajax({
type: 'post',
url: "Http://localhost:8080/user/register",
data: JSON.stringify(data),
contentType : 'application/json',
dataType : 'json',
success: function (res) {
if (res.code==200) {
layer.msg('注册成功', function () {
window.location = 'login-3.html';
});
} else {
layer.alert(res.msg, {icon: 5}, function (index) {
layer.close(index);
});
}
}
});
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgchgie
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01