ajax使用POST方法提交表单

来源:互联网 发布:php企业网站模版 编辑:程序博客网 时间:2024/05/22 03:12
 

ajax中使用post 方式提交表单时能提交多达2GB的内容,而GET方法只能提交最多512KB的内容.以下是ajax POST提交的例子.

 

[c-sharp] view plaincopyprint?
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" >  
  3. <head>  
  4.     <title>Ajax POST方法提交表单</title>  
  5.     <mce:script type="text/javascript"><!--  
  6.     window.onerror=function(errorMessage,errorUrl,errorNum)  
  7.     {  
  8.         alert(errorMessage+errorUrl+errorNum);  
  9.     }  
  10.      var xmlHttp;  
  11.      function createXmlHttp()  
  12.      {  
  13.         if(window.ActiveXObject)  
  14.          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");  
  15.         else  
  16.          xmlHttp=new XMLHttpRequest();  
  17.      }  
  18.      function startRequest()  
  19.      {  
  20.       try  
  21.       {  
  22.          createXmlHttp();  
  23.          var url="ajax_post.ashx";  
  24.          var postedData=getRequestBody(document.forms["form1"]);  
  25.           
  26.          xmlHttp.open("post",url,true);  
  27.          xmlHttp.setRequestHeader("content-length",postedData.length);//post提交设置项  
  28.          xmlHttp.setRequestHeader("content-type","application/x-www-form-urlencoded");//post提交设置项  
  29.          xmlHttp.onreadystatechange =onComplete;  
  30.          //将名值对发送到服务器   
  31.          xmlHttp.send(postedData);  
  32.        }  
  33.        catch(e)  
  34.        {  
  35.          alert(e.message);  
  36.        }   
  37.      }  
  38.      function onComplete()  
  39.      {  
  40.         if(xmlHttp.readyState==4&&xmlHttp.status==200)  
  41.          {  
  42.             //显示结果   
  43.             document.getElementById("divResult").innerText=xmlHttp.responseText;  
  44.          }  
  45.      }  
  46.      //获取表单中的名值对   
  47.      function getRequestBody(oForm)  
  48.      {  
  49.         var aParams=new Array();  
  50.         for(var i=0;i<oForm.elements.length;i++)  
  51.         {  
  52.           var sParam=encodeURIComponent(oForm.elements[i].id)  
  53.           sParam+="=";  
  54.           sParam+=encodeURIComponent(oForm.elements[i].value);  
  55.           aParams.push(sParam);  
  56.         }  
  57.         return aParams.join("&");  
  58.      }  
  59. // --></mce:script>   
  60. </head>  
  61. <body>  
  62. <form id="form1">  
  63.     要提交的字段落1:  
  64.     <input id="Text1" type="text" /><br />  
  65.     要提交的字段落2:  
  66.     <input id="Text2" type="text" />  
  67.     <br />  
  68.     <input id="Button1" type="button" value="POST提交" onclick="startRequest();" /><div id="divResult"></div></form>  
  69. </body>  
  70. </html>  
  

服务器端处理代码(ajax_post.ashx):

[c-sharp] view plaincopyprint?
  1. <%@ WebHandler Language="C#" Class="ajax_post" %>  
  2.   
  3. using System;  
  4. using System.Web;  
  5.   
  6. public class ajax_post : IHttpHandler {  
  7.       
  8.     public void ProcessRequest (HttpContext context)   
  9.     {  
  10.         context.Response.ContentType = "text/plain";  
  11.         context.Response.Write(String.Format("你输入的值是{0}和{1}!",context.Request.Form["Text1"],context.Request.Form["Text2"]));  
  12.     }  
  13.    
  14.     public bool IsReusable   
  15.     {  
  16.         get {  
  17.             return false;  
  18.         }  
  19.     }  
  20.   
  21. }  

原创粉丝点击