码迷,mamicode.com
首页 > Web开发 > 详细

ajax请求

时间:2019-09-12 13:03:25      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:new   ati   reload   func   uri   catch   style   result   tip   

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请求

标签:new   ati   reload   func   uri   catch   style   result   tip   

原文地址:https://www.cnblogs.com/lazyli/p/11511147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!