AJAX GET和POST传递参数

 

 
    
1 < script language = " javascript " type = " text/javascript " >
2 var _xmlHttpRequest ;
3 function createXmlHttpRequest() {
4 return window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject( " Microsoft.XMLHTTP " ) : new XMLHttpRequest();
5 }
6
7 function valieUserByGet() {
8 var _userName = document.getElementById( " Text1 " );
9 var _passWord = document.getElementById( " Text2 " );
10 _xmlHttpRequest = createXmlHttpRequest();
11 if (_xmlHttpRequest != null ) {
12 var _url = " Login_System.aspx?username= " + escape(_userName.value) + " &password= " + escape(_passWord.value);
13 var _method = " GET " ;
14 _xmlHttpRequest.open(_method, _url, true );
15 _xmlHttpRequest.onreadystatechange = handleStateChange;
16 _xmlHttpRequest.send( null );
17 }
18 }
19
20 function valieUserByPost() {
21 var _userName = document.getElementById( " Text1 " );
22 var _passWord = document.getElementById( " Text2 " );
23 _xmlHttpRequest = createXmlHttpRequest();
24 if (_xmlHttpRequest != null ) {
25 var _url = " Login_System.aspx " ;
26 var _method = " POST " ;
27 _xmlHttpRequest.open(_method, _url, true );
28 _xmlHttpRequest.onreadystatechange = handleStateChange;
29 _xmlHttpRequest.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded " );
30 _xmlHttpRequest.send( " username= " + escape(_userName.value) + " &password= " + escape(_passWord.value));
31 }
32 }
33
34 function handleStateChange() {
35 if (_xmlHttpRequest.readyState == 4 ) {
36 if (_xmlHttpRequest.status == 200 ) {
37 var _responseText = _xmlHttpRequest.responseText;
38 if (_responseText == " 0 " ) {
39 alert( " 用户名不存在 " );
40 }
41 else if (_responseText == " 1 " ) {
42 alert( " 密码不正确 " );
43 }
44 else if (_responseText == " 2 " ) {
45 alert( " 验证通过 " );
46 }
47 }
48 }
49 }
50 < / script>

 

 
    
1 protected void Page_Load( object sender, EventArgs e)
2 {
3
4 string _userName = Request[ " username " ] != null ? Request[ " username " ].ToString() : "" ;
5 string _passWord = Request[ " password " ] != null ? Request[ " password " ].ToString() : "" ;
6 if (_userName != "" && _passWord != "" )
7 {
8 var _responseText = "" ;
9 if (_userName.ToLower() == " admin " )
10 {
11 if (_passWord.ToLower() == " 111111 " )
12 {
13 _responseText = " 2 " ;
14 }
15 else
16 {
17 _responseText = " 1 " ;
18 }
19 }
20 else
21 {
22 _responseText = " 0 " ;
23 }
24 writeRespose(_responseText);
25 }
26
27 }
28
29 private void writeRespose( string _writeResponse)
30 {
31 Response.Clear();
32 Response.ContentType = " text/html " ;
33 Response.CacheControl = " no-cache " ;
34 Response.Write(_writeResponse);
35 Response.End();
36 }

 

 
    
1 < div >
2 用户名 < input id ="Text1" type ="text" />
3 div >
4 < div >
5 密码 < input id ="Text2" type ="text" />
6 div >
7 < div >
8 < input id ="Button1" type ="button" value ="get验证" onclick ="valieUserByGet();" />
9 < input id ="Button2" type ="button" value ="post验证" onclick ="valieUserByPost();" />
10 div >

 

转载于:https://www.cnblogs.com/Tim_Liu/archive/2010/04/08/1707208.html

你可能感兴趣的:(AJAX GET和POST传递参数)