ajax传参数json对象到后台获取

 

类型1

var version = $("#version").val();
var ids[i] = ("127.0.0.1","192.168.1.1","192.168.3.3");
$.ajax({
    type: 'POST',
    data: JSON.stringify({
        'ips': ids,
        'version': version
    }),
    url: prefix + '/sendFile',
    contentType: "application/json",
    dataType: "JSON",
    error : function(request) {
        parent.layer.alert("Connection error");
    },
    success: function (r) {
        if (r.code == 0) {
            layer.msg(r.msg);
        } else {
            layer.msg(r.msg);
        }
    }
});


@ResponseBody
@PostMapping("/sendFile")
public void sendFile(@RequestBody JSONObject jsonObject) {
JSONArray ips=jsonObject.getJSONArray("ips");
String version = jsonObject.getString("version");
String path=updateSendService.getFilePathByVersion(version);

for (int i = 0; i < ips.size(); i++) {
System.err.println(ips.get(i));
System.err.println(version);
    }
}


类型2

<form action="">
First name: <input type="text" name="FirstName" value="Bill" /><br />
Last name: <input type="text" name="LastName" value="Gates" /><br />
</form>
$(document).ready(function(){
    console.log($("form").serialize()); // FirstName=Bill&LastName=Gates
});

这样,我们就可以把序列化的值传给ajax()作为url的参数,轻松使用ajax()提交form表单了,而不需要一个一个获取表单中的值然后传给ajax(),举例如下:

 

$.ajax({
    type: 'post',
    url: '/update/manage/save', 
   data: $(
"form").serialize(),
   success:
function(data) { // your code } });
@ResponseBody
    @PostMapping("/save")
    public R save(UpdateManageDO update){//UpdataMannageDO是Form对应的映射类
        if(updateManage.save(update)>0){
            return R.ok();
        }
        return R.error();
    }

更多传值方式参考:https://www.cnblogs.com/smfx1314/p/10205736.html
posted @ 2019-12-18 20:31  指尖下的世界  阅读(7979)  评论(1编辑  收藏  举报
/* 看板娘 */ /*炸泡*/
/* 鼠标点击求赞文字特效 */