实现效果:输入用户名和年龄,点击按钮,可把数据提交到后台(如上图:数据被提交到myeclipse的console里面)。
第一个按钮是用js方法提交,第二个按钮用jquery方法。
用到的工具:webstorm+myeclipse
Title用户名:
年龄:
传数据到后端
jquery的ajax提交
//js传送方法
function get() {
var name = document.getElementById("name").value;
var age = document.getElementById("age").value;
var xml = new XMLHttpRequest();
xml.open("get","http://localhost:8080/Test/servlet/TestServlet?one="+name+"&two="+age,true);
xml.onreadystatechange = function () {
};
xml.send();
}
//jquery提交的ajax
$("#jquery").click(function () {
var name = $("#name").val();
var age= $("#age").val();
//第一种传参数的写法
$.get("http://localhost:8080/Test/servlet/TestServlet?one="+name+"&two="+age,function (data) {
console.log(data);
})
//第二种传参数的写法
$.get("http://localhost:8080/Test/servlet/TestServlet",{one:name,two:age},function (data) {
console.log(data);
})
})