首页  

jquery ajax 实例     所属分类 jquery 浏览量 505
jquery-3.5.1.js
get  , post  ,post json 例子

ajax几个重要参数
// 默认 get
type: "post"

// contentType 默认 application/x-www-form-urlencoded
contentType: "application/json; charset=utf-8",
// 默认 true ,异步请求
async: false,

post json 时
data 数据一定要转成 json string 
data: JSON.stringify(user3),


服务端 rest 接口 @RequestMapping("/getUser") public User getUser(User user){ return user; } @RequestMapping("/getUserFromJson") public User getUserFromJson(@RequestBody User user){ return user; }
function getData() { // alert('hello2'); var user = { "id": 9, "name": "tiger" }; alert('hello,' + JSON.stringify(user)); // ajax get $.ajax({ url: "/data/getUser", data: user, success: function (data) { console.log(data); $('#data1').html(JSON.stringify(data)); } }); // ajax post var user2 = { "id": 9, "name": "tiger2" }; $.ajax({ type: "post", url: "/data/getUser", data: user2, success: function (data) { console.log(data); $('#data2').html(JSON.stringify(data)); } }); // ajax post json var user3 = { "id": 9, "name": "tiger3" }; $.ajax({ // 默认 get type: "post", // contentType 默认 application/x-www-form-urlencoded contentType: "application/json; charset=utf-8", // 预期的服务器响应的数据类型 ,可以不写 // dataType: "json", url: "/data/getUserFromJson", // 一定要转成string data: JSON.stringify(user3), // data:user3, success: function (data) { console.log(data); $('#data3').html(JSON.stringify(data)); } }); }
完整代码 https://gitee.com/dyyx/springboothello/blob/master/src/main/resources/static/jquery_ajax_post.html https://gitee.com/dyyx/springboothello/blob/master/src/main/java/demo/controller/DataBindController.java

上一篇     下一篇
Javascript strip 去除首尾指定的字符串

jQuery 实用代码

使用 netcat 查看 http 请求信息

XMLHttpRequest 例子

ssh端口转发

PostgreSQL DBA 常用SQL