javascript 如何用POST方式(以及Get方式) 向服务器端提交数据


客户端:

 1  < script  type ="text/javascript" >
 2       // 下面的代码使用Post和Get两种方式向服务器提交数据
 3       // 这里做了一个简单的重构,两种方式调用的时候只要传递HttpVerb就可以了
 4       function  getData(verb)
 5      {
 6           var  webRequest = new  Sys.Net.WebRequest();
 7           if  (verb == " POST " )
 8          {
 9            webRequest.set_url( " ProcessRequest.aspx " );
10          }
11           else
12          {
13           webRequest.set_url( " ProcessRequest.aspx?AID= " + $get( " aidTextbox " ).value);
14          }
15          webRequest.add_completed(OnCompleted);
16          
17          webRequest.set_httpVerb(verb);
18           var  requestBody = " AID= " + $get( " aidTextbox " ).value;
19          webRequest.set_body(requestBody);
20          webRequest.get_headers()[ " Conten_Length " ] = requestBody.length;
21          webRequest.invoke();
22      }
23       function  getDataByPost()
24      {
25          getData( " POST " );
26      }
27       function  getDataByGet()
28      {
29          getData( " Get " );
30      }
31       // 完成之后调用的参数 注意一下参数
32       function  OnCompleted(executor,eventArgs)
33      {
34           if (executor.get_responseAvailable()) 
35          {
36              $get( " data " ).innerHTML = executor.get_responseData();
37          }
38      }
39       </ script >
40 
41       < div  id ="data" >
42       </ div >
43       < input  id ="aidTextbox"  type ="text"  value ="me_sa"   />
44       < input  id ="Button1"  type ="button"  value ="Post"  onclick ="javascript:getDataByPost();"   />
45       < input  id ="Button2"  type ="button"  value ="Get"  onclick ="javascript:getDataByGet();"   />

ProcessRequest.aspx服务器端代码:
 1  using  System;
 2  using  System.Collections;
 3  using  System.Configuration;
 4  using  System.Data;
 5  using  System.Web;
 6  using  System.Web.Security;
 7  using  System.Web.UI;
 8  using  System.Web.UI.HtmlControls;
 9  using  System.Web.UI.WebControls;
10  using  System.Web.UI.WebControls.WebParts;
11 
12  public  partial  class  ProcessRequest : System.Web.UI.Page
13  {
14       protected   void  Page_Load( object  sender, EventArgs e)
15      {
16           string  temp  =   "" ;
17          Response.Clear();
18           if  ( ! string .IsNullOrEmpty(Request.Form[ " AID " ]))
19          {
20              temp  = DateTime.Now.ToString() +   "  POST过来的数据是: "   +  Request.Form[ " AID " ].ToString();
21          }
22           if  ( ! string .IsNullOrEmpty(Request.QueryString[ " AID " ]))
23          {
24              temp  =  DateTime.Now.ToString()  +   " GET过来的数据是: "   +  Request.QueryString[ " AID " ].ToString();
25          }
26 
27          Response.Write(temp);
28          Response.End();
29          
30      }
31  }
32 

就说这么多,代码下载地址: http://files.cnblogs.com/me-sa/AjaxTest.rar
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值