XMLHttpRequest的POST方式发送

来源:互联网 发布:php 分享到朋友圈代码 编辑:程序博客网 时间:2024/05/21 20:21

注意:在HTML代码中一定要有xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");  //用POST的时候一定要有这句,用GET的时候可以不用这句
我再补充一点,用XMLHttpRequest想后台发送的时候,是没有什么Page.IsPostBack的,即他总是认为是第一次加载。

 Default.asp

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    
<title>无标题页</title>
<script language="javascript" type="text/javascript">
<!--
var xmlhttp;
function Button1_onclick() 
{
    
var dzb = "info={'FullName':'王继军','Company':'上海天正','Address':'田林路388号'}";
    
//"Departments":["总经理室","酒店事业部","市场销售部","金融事业部"]
    send(dzb);
}

    
function send(arg)
    
{
       CreateXMLHttpRequest();
       xmlhttp.onreadystatechange 
= callhandle;
       
//xmlhttp.open("GET","Default.aspx?goback=yes&arg=" + arg,true);
       xmlhttp.open("POST","Default.aspx?goback=yes",true);
       xmlhttp.setRequestHeader(
"Content-Length",arg.lenght);
       xmlhttp.setRequestHeader(
"Content-Type","application/x-www-form-urlencoded;");  //用POST的时候一定要有这句
       xmlhttp.send(arg);
       
    }

    
function CreateXMLHttpRequest()
    
{
        
if (window.ActiveXObject)
        
{
            xmlhttp 
= new ActiveXObject("Microsoft.XMLHTTP");
        }

        
else if (window.XMLHttpRequest)
        
{
            xmlhttp 
= new XMLHttpRequest();
        }

    }

    
function callhandle()
    
{
        
if (xmlhttp.readyState == 4)
        
{
            
if(xmlhttp.status == 200)
            
{
                
var dzb = eval("(" + xmlhttp.responseText +")");
                alert(dzb.Address);
            }

        }

    }


// -->
</script>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<br />
        
<br />
        
<input id="Button1" type="button" value="button" language="javascript" onclick="return Button1_onclick()" />&nbsp;</div>
    
</form>
</body>
</html>

Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Newtonsoft.Json;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

        System.IO.StreamReader red 
= new System.IO.StreamReader(Request.InputStream, System.Text.Encoding.UTF8);/*有可能是gb2312*/
        
string ss = red.ReadToEnd();
        
try
        
{
            
if (Request.QueryString.Keys.Count == 0)
                
return;
            
if (Request.QueryString["goback"].ToString() == "yes")
            
{
                AddressBook dbs 
= (AddressBook)JavaScriptConvert.DeserializeObject(Request.Form["info"].ToString(), typeof(AddressBook));
                dbs.Address 
= "haha";
                
string output = JavaScriptConvert.SerializeObject(dbs);
                Response.Write(output);
                Response.Flush();
                Response.Close();
            }

        }

        
catch(Exception ex)
        
{
            System.Console.Write(ex.Message);
        }

    }

}

public class AddressBook
{
    
string _fullName = "wjj";
    
string _company = "tianzheng";
    
string _address = "田林路";
    
    
public string FullName
    
{
        
get return _fullName; }
        
set { _fullName = value; }

    }

    
public string Company
    
{
        
get return _company; }
        
set { _company = value; }
    }

    
public string Address
    
{
        
get return _address; }
        
set { _address = value; }
    }

}

原创粉丝点击