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()" /> </div>
</form>
</body>
</html>
<!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()" /> </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; }
}
}
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; }
}
}
- XMLHttpRequest的POST方式发送
- XMLHttpRequest发送POST请求
- XmlHttpRequest的Post请求方式示例
- xmlHttpRequest的post方法
- C#发送Post方式的Http请求
- 发送post请求(表单的方式)
- 使用post的方式发送http请求
- POST方式发送数据
- post方式发送数据
- Firefox3中使用XMLHttpRequest(Ajax)发送二进制POST数据的简易调试法
- 使用JavaScript的XMLHttpRequest发送POST、GET请求以及接收返回值
- 使用JavaScript的XMLHttpRequest发送POST、GET请求以及接收返回值
- 使用JavaScript的XMLHttpRequest发送POST、GET请求以及接收返回值
- 原始ajax通过xmlHttpRequest对象的send()方法提交数据--Get方式和Post方式
- xmlHttpRequest 以Post方式发数据到Asp.net页,在gb2312编码下的解决办法
- POST方式发送XML数据
- Post方式发送Ajax请求
- 以post方式发送链接请求的javascript函数
- C#的方法参数-Params,Ref,Out
- SOAP&Web Service
- 2006-09-26 13:00Summary摘要给加拿大的一位朋友做的网站 http://www.mantique.ca/new%20mail
- 凤凰古城-湘西之行
- 最近讨厌的一句话"希望理解我"
- XMLHttpRequest的POST方式发送
- 找工作 -java程序员
- 如何获取当前程序文件的路径 Current Path
- 关于进程wuauclt.exe
- 在POWER平台的Linux如何使用XL C/C++
- 通过FileSystemObject在网页中操作本地文件
- 开发人员一定要加入收藏夹的网站
- VC网络资源
- 试试怎么用