1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title></head><body> <form id="login" method="post" action="result.jsp"> <input name="user" type="text"/> <input name="sex" type="radio" value="man"/> <input name="sex" type="radio" value="woman"/> interest: <input type="checkbox" name="interest" value="piu">PIU <input type="checkbox" name="interest" value="dss">DSS <input type="checkbox" name="interest" value="ddr">DDR<br> <input type="button" name="submit" value="submit" onclick="getFormInfo();"> </form> <script type="text/javascript" src="./../js/jquery.min.js"></script> <script type="text/javascript" src="serializeJSON.js"></script> <script type="text/javascript" src="test.js"></script> <script type="text/javascript"> //第一种写法(拼接URL) function getFormInfo(){ var name='wen'; var user='chen'; $.ajax({ url: "/login/authenticate?name="+name+"&user="+user, type: "POST", data:{}, dataType: "json", success: function(data){ }, error:function(err){ console.log(err.statusText); console.log('异常'); } }); } //第二种写法(表单序列化为json数据) function getFormInfo(){ var params=$('#login').serializeJSON(); console.log(params); $.ajax({ url: "http://192.168.10.32:6833/login/authenticate", type: "POST", data:params, cache:false, dataType: "json", success: function(data){ }, error:function(err){ } }); } // 第三种写法(表单序列化为字符串) function getFormInfo(){ var params=$('#login').serialize(); console.log(params); $.ajax({ url: "http://192.168.10.32:6833/login/authenticate", type: "POST", data:params, cache:false, dataType: "json", success: function(data){ }, error:function(err){ } }); } //第四种写法(带json数据) function getFormInfo(){ $.ajax({ url: "http://192.168.10.32:6833/login/authenticate", type: "POST", data:{ name:'chem', user:'wen' }, cache:false, dataType: "json", success: function(data){ }, error:function(err){ } }); } //第五种写法(拼接data) function getFormInfo(){ var name='chen'; var user='wen'; $.ajax({ url: "http://192.168.10.32:6833/login/authenticate", type: "POST", data:'name='+name+'&user='+user, cache:false, dataType: "json", success: function(data){ }, error:function(err){ } }); } //第六种写法(既有全部直接获取表单中的数据又有单独出来的数据) function getFormInfo(){ var params=$('#login').serializeJSON(); console.log(params); params.height='20'; $.ajax({ url: "http://192.168.10.32:6833/login/authenticate", type: "POST", data:params, cache:false, dataType: "json", success: function(data){ }, error:function(err){ } }); } </script></body></html> |
ajax请求时data数据格式
最新推荐文章于 2026-04-20 11:50:52 发布
本文介绍了一种使用HTML表单与AJAX进行数据提交的方法,通过不同方式发送表单数据到服务器,包括拼接URL参数、序列化表单为JSON数据等。同时还展示了如何使用jQuery库来辅助实现这些功能。

478

被折叠的 条评论
为什么被折叠?



