ajax发送get、post请求

ajax可以发送post或者get请求,并且可以设置同步或者异步,这里罗列了几种。其中,后台处理请求由servlet实现。

第一种方式:

var xmlhttp = new XMLHttpRequest();
//发送post请求,false表示发送同步请求
xmlhttp.open("POST","AdminLogin",false);
//设置文件的头,UTF-8应与html编码格式一致,告诉浏览器对参数编码的格式,可以解决中文传输乱码的问题
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=UTF-8");
//设置传递的参数
xmlhttp.send("id="+id+"&password="+password);
//显示返回结果
alert(xmlhttp.responseText);


第二种方式(接受JSON包):

//使用ajax方法,JS代码
$.ajax({
    url: "GetStudentInfo",
    type: 'POST',
    async: false,
    contentType: 'application/json',
    mimeType: 'application/json',
    success: function (data) {//对返回值的处理
		$("#id").val(data.id);
		$("#name").val(data.name);
		$("#year").val(data.year);
		$("#specialty").val(data.specialty);
		$("#phone").val(data.phone);
		$("#email").val(data.email);
    },
});

//Servlet代码
public class User {
	public String id;
	public String name;
	public String year;
	public String specialty;
	public String phone;
	public String email;
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
	ObjectMapper mapper = new ObjectMapper();
        Connection con = DBTool.getConnection();
	User u = new User();
	u.id = "a";
	u.name = "b";
	u.year = "c";
	u.specialty = "d";
	u.phone = "e";
	u.email = "f";
	response.setContentType("application/json");  
}

第三种方式(接受JSON包,返回值为集合):

//JS代码
$.ajax({
      url: "CheckAllStudent",
      type: 'POST',
      contentType: 'application/json',
      mimeType: 'application/json',
      success: function (data) {
          $.each(data, function (index, student) {
        	var string = "<tr>";
        	string += "<td>" + student.id+ "</td>";
        	string += "<td>" + student.name+ "</td>";
        	string += "<td>" + student.year+ "</td>";
        	string += "<td>" + student.specialty+ "</td>";
        	string += "<td>" + student.phone+ "</td>";
        	string += "<td>" + student.email+ "</td>";
        	$("tbody").append(string);
          }); 
      },
});

//Servlet代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException {
    	ObjectMapper mapper = new ObjectMapper();
    	//把相同的对象放入List中
    	List<User>	list = new ArrayList<User>();
    	User u1,u2,u3;
    	list.add(u1);
    	list.add(u2);
    	list.add(u3);
	response.setContentType("application/json");  
	mapper.writeValue(response.getOutputStream(), list);
}


第四种方式(ajax方法,带参数):

$.ajax({
	url: "ShowAdvertise",
	type: 'POST',
	data: {time:time},
	success: function(data) {
		alert(data);
	},
});

参考文献:

jQuery ajax - ajax() 方法





  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值