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();" />
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
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
就说这么多,代码下载地址:https://files.cnblogs.com/me-sa/AjaxTest.rar
作者: XuGang 网名:钢钢 |
出处: http://xugang.cnblogs.com |
声明: 本文版权归作者和博客园共有。转载时必须保留此段声明,且在文章页面明显位置给出原文连接地址! |