`

[JQuery]如何使用POST/GET

阅读更多

Post:

$(function(){
			
			var responseMsg = $('#responseMsg');
			responseMsg.html('');
			
			$('#loginBt').click(function(){
				var username = $('#username').val();
				var password = $('#password').val();
				var url = "login.do";
				$.post(url, {account:username,password:password}, function(data, status){
					console.log('data: ' + data)
					if(data.description == 'Success'){						
						var strFullPath=window.document.location.href; 												
						var strPath=window.document.location.pathname; 						
						var pos=strFullPath.indexOf(strPath); 						
						var prePath=strFullPath.substring(0,pos); 						
						var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);						
						window.location.href=prePath + postPath + '/page/home.html';											
						} else {						
							responseMsg.html('Invalid username or password');					
						}
				})
			})
		});

 

Get:

var url = "login.do?username="+username+"&password="+password;
				$.get(url, function(data, status){
					if(data.description == 'Success'){
						var strFullPath=window.document.location.href; 
						
						var strPath=window.document.location.pathname; 
						var pos=strFullPath.indexOf(strPath); 
						var prePath=strFullPath.substring(0,pos); 
						var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);
						window.location.href=prePath + postPath + '/page/home.html';
						
					} else {
						responseMsg.html('Invalid username or password');
					}
				})

 

 ajax post:

$.ajax({
		url : url,
		beforeSend : function(){
			ajaxLoading();
		},
		type : 'POST',
		data : {
			submit_time_from : submitTimeFrom,
			submit_time_to : submitTimeTo,
			corp_id : corpId,
		
		},
		success : function(data, status){
			ajaxLoadEnd();
			if(data == 'Please login first'){
				responseMsg.html(data);
				return;
			}
			
			var statusCode = data.code;
			if (statusCode == '0') {
				responseMsg.html(data.description);
			} else {
				responseMsg.html(data.description);
			}
		}
		
	});

 

function ajaxLoading(){
  $("<div class=\"datagrid-mask\"></div>").css({display:"block",width:"100%",height:$(window).height()}).appendTo("body");   
  $("<div class=\"datagrid-mask-msg\"></div>").html("Being handling, please wait...").appendTo("body").css({display:"block",left:($(document.body).outerWidth(true) - 190) / 2,top:($(window).height() - 45) / 2});   
}   

function ajaxLoadEnd(){ 
   $(".datagrid-mask").remove();   
   $(".datagrid-mask-msg").remove();               
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics