首页 > 代码库 > ajax传输数据

ajax传输数据

AJAX向后台传输数//1 直接在url中传递

$.ajax({

type: "POST",

url: "/testAjax/addUser?id=1&name=zhangxiaofan&sex=male",

success:function(data){

//TODO

}

});

//2 html页面from表单序列化

var formData = http://www.mamicode.com/$("#form").serialize();

$.ajax({

type: "POST",

url: "/testAjax/addUser",

data: formData,

success:function(data){

//TODO

}

});

//3 拼接data字符串

var formData = http://www.mamicode.com/$("#form").serialize();

$.ajax({

type: "POST",

url: "/testAjax/addUser",

data: "id=1&name=zhangxiaofan&sex=male",

success:function(data)

}

});

//4 简单的js对象

var userData = http://www.mamicode.com/{

id:1,

name:zhangxiaofan,

sex:male

};

$.ajax({

type: "POST",

url: "/testAjax/addUser",

data: userData, //或者data:{id:1,name:zhangxiaofan,sex:male}

success:function(data){

//TODO

}

})
拼接data法详解:

http://simple.logphp.com/?a=url&k=6668b1fc&u=aHR0cDovL3d3dy5jbm

ajax传输数据