用jQuery实现Ajax

jQuery.ajax([settings])

type : "POST"或"GET"

url : 发送请求的地址

data : 对象,连同请求发送到服务器的数据

dataType : 浏览器返回数据,一般设为"json"

success : 请求成功后的回调函数

error : 请求失败时调用函数,传入XMLHttpRequest对象

$(document).ready(function() {
	$("#search").click(function() {
		$.ajax({
			url: '/path/to/file',
			type: 'GET',          //对get方法的改写
			dataType: 'json',
			success : function (data) {
				if (data.success) {
					$("#result").html(data.msg);
				}else {
					$("#result").html("error:"+data.msg);
				}
			};
			error : function(XHR) {
				alert("ERROR");
			};
		})
	});
});
$(document).ready(function() {
	$("#search").click(function() {
		$.ajax({
			url: '/path/to/file',
			type: 'POST',        //对POST方法的改写
			dataType: 'json',
			data:{
				name:$("#formname").val(),
				number:$("#formnumber").val(),
				sex:$("#formsex").val(),
				job:$("#formjob").val(),
			}
			success : function (data) {
				if (data.success) {
					$("#result").html(data.msg);
				}else {
					$("#result").html("error:"+data.msg);
				}
			};
			error : function(XHR) {
				alert("ERROR");
			};
		})
	});
});


猜你喜欢

转载自blog.csdn.net/qq_41443301/article/details/81058426