jquery ajax格式

jquery ajax的请求格式

$.ajax({
	type : 'post',//post或者get
	url : url,
	async : false,//是否异步提交 默认为true
	global : false,//阻止ajax全局属性生效,比如下面的ajaxStart和ajaxStop
	data : {
		"id" : id,
		"name" : name,
		"title" : title
	},//json格式
	//data : 'id='+$('#id').val(),//字符串格式
	dataType : 'html', //json
	success : function(msg){ 
		//数据处理
	},
	error : function(msg){
		console.log(msg);//输出报错信息
	} 
});

ajax请求发起和完成时调用的方法,ajax请求发起时,显示遮罩层,请求完成后,关闭遮罩层

	<div id="ajaxShowBg" style="width: 100%; height: 100%; position: fixed; z-index: 1988; top: 0; left: 0; overflow: hidden; display: none;">
		<div style="height: 100%; text-align: center; opacity: 0.5; background: #ECE8DE; padding-top: 20%;">
			<div style="">
				<img src="/images/loading3.gif"/>
			</div>
		</div>
	</div>
	<script>
		$(function(){
			// jquery 1.8以上只能绑定在document上,这里需要注意下
			$(document).ajaxStart(function(){
				$("#ajaxShowBg").show();
			}).ajaxStop(function(){
				$("#ajaxShowBg").hide();
			}).ajaxError(function(e,xhr,opt){
	            console.log(e,xhr,opt);
	            $("#ajaxShowBg").hide();
	        });
		});
	</script>

更多的ajax方法可以参考:

http://api.jquery.com/jQuery.ajax/
http://www.w3school.com.cn/jquery/jquery_ref_ajax.asp

发布了4 篇原创文章 · 获赞 1 · 访问量 2094

猜你喜欢

转载自blog.csdn.net/cyycaoyang/article/details/51321510