ajax请求
Posted lazyli
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ajax请求相关的知识,希望对你有一定的参考价值。
1、比较正常的ajax请求;传递到后台的参数是id:1;形式
function alertModel(id)
console.log("修改的id:"+id);
$.ajax(
url: "",
type: "post",
data:
id: id
,
success:function(data)
console.log(data);
);
2、请求的时候,参数进行处理
function saveModel()
var p = [];
var temp = ;
temp.modelStandardName = $("#modelStandardName").val();
temp.modelFactory = $("#modelFactory").val();
temp.modelBrand = $("#modelBrand").val();
temp.modelCateName = $("#modelCateName").val();
temp.modelYear = $("#modelYear").val();
temp.modelClass = $("#modelClass").val();
temp.modelType = $("#modelType").val();
temp.modelCountry = $("#modelCountry").val();
temp.modelVouchSeat = $("#modelVouchSeat").val();
temp.modelRatedLoad = $("#modelRatedLoad").val();
temp.modelFactoryDirectPrice = $("#modelFactoryDirectPrice").val();
temp.remark = $("#remark").val();
p.push(temp);
layer.close(1);
currTc = layer.load(currTc);
$.ajax(
url:"<%=basePath%>commerical/addModel",
type:"post",
data:
params: encodeURI(JSON.stringify(p))
,
success:function(msg)
layer.closeAll();
if (msg.success)
layer.close(currTc);
layer.confirm(‘添加成功‘,
btn: [‘确定‘], //按钮
yes:function()
window.location.reload();
);
else
currTc = 0;
errorTip(msg, layer,msg.msg);
)
后台接收以String,接收参数;需要进行处理
@RequestMapping("/addModel")
@ResponseBody
public AjaxResult addModel(String params)
AjaxResult ajaxResult = new AjaxResult();
try
params = URLDecoder.decode(params,"utf-8" );
JSONArray jsonArray = JSON.parseArray(params);
System.out.println(jsonArray);
int result = commericalModelService.addModel(jsonArray);
if(result > 0)
ajaxResult.setSuccess(true);
else
ajaxResult.setSuccess(false);
catch (Exception e)
e.printStackTrace();
ajaxResult.setSuccess(false);
return ajaxResult;
以上是关于ajax请求的主要内容,如果未能解决你的问题,请参考以下文章