ajax中使用post 方式提交表单时能提交多达2GB的内容,而GET方法只能提交最多512KB的内容.以下是ajax POST提交的例子.
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>AjaxPOST方法提交表单</title>
- <mce:scripttype="text/javascript"><!--
- window.onerror=function(errorMessage,errorUrl,errorNum)
- {
- alert(errorMessage+errorUrl+errorNum);
- }
- varxmlHttp;
- functioncreateXmlHttp()
- {
- if(window.ActiveXObject)
- xmlHttp=newActiveXObject("Microsoft.XMLHTTP");
- else
- newXMLHttpRequest();
- functionstartRequest()
- try
- createXmlHttp();
- varurl="ajax_post.ashx";
- varpostedData=getRequestBody(document.forms["form1"]);
-
- xmlHttp.open("post",url,true);
- xmlHttp.setRequestHeader("content-length",postedData.length);
- xmlHttp.setRequestHeader("content-type","application/x-www-form-urlencoded");
- xmlHttp.onreadystatechange=onComplete;
-
- xmlHttp.send(postedData);
- }
- catch(e)
- alert(e.message);
- functiononComplete()
- if(xmlHttp.readyState==4&&xmlHttp.status==200)
- //显示结果
- document.getElementById("divResult").innerText=xmlHttp.responseText;
- //获取表单中的名值对
- functiongetRequestBody(oForm)
- varaParams=newArray();
- for(vari=0;i<oForm.elements.length;i++)
- varsParam=encodeURIComponent(oForm.elements[i].id)
- sParam+="=";
- sParam+=encodeURIComponent(oForm.elements[i].value);
- aParams.push(sParam);
- returnaParams.join("&");
-
- </head>
- <body>
- <formid="form1">
- 要提交的字段落1:
- <inputid="Text1"type="text"/><br/>
- 要提交的字段落2:
- <inputid="Text2"type="text"/>
- <br/>
- <inputid="Button1"type="button"value="POST提交"onclick="startRequest();"/><divid="divResult"></div></form>
- </body>
- </html>
服务器端处理代码(ajax_post.ashx):
copy
<%@WebHandlerLanguage="C#"Class="ajax_post"%>
-
- usingSystem;
- usingSystem.Web;
- publicclassajax_post:IHttpHandler{
- voidProcessRequest(HttpContextcontext)
- context.Response.ContentType="text/plain";
- context.Response.Write(String.Format("你输入的值是{0}和{1}!",context.Request.Form["Text1"],context.Request.Form["Text2"]));
- boolIsReusable
- get{
- returnfalse;
- }
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|