=波波日志 > JavaScript/Ajax > ajax指定为post但是未设置content-type或未指定键时时如何获取提交的内容=
ajax指定为post但是未设置content-type或未指定键时时如何获取提交的内容
1)要生成键值对形式,你得指定content-type为“application/x-www-url-encoded”
+展开
-JavaScript
xhr.open("post","动态页",true);
xhr.setRequestHeader("content-type","application/x-www-url-encoded");//注意要在open后才能调用setRequestHeader
xhr.setRequestHeader("content-type","application/x-www-url-encoded");//注意要在open后才能调用setRequestHeader
这样就可以在动态页使用
+展开
-C#
string v=Request.Form["键"];
asp
v=Request.Form("键")
来获取对应的键值。
2)在发送数据时未指定键,则可以使用Request.Form.ToString()获取提交的内容
+展开
-JavaScript
xhr.open("post","test.aspx",true);
xhr.setRequestHeader("content-type","application/x-www-url-encoded");
xhr.send("123456789");
xhr.setRequestHeader("content-type","application/x-www-url-encoded");
xhr.send("123456789");
+展开
-C#
string v=Request.Form.ToString();//得到123456789
asp
v=request.form
3)未设置content-type或者content-type设置成非application/x-www-url-encoded,则需要读取2进制流数据
+展开
-JavaScript
xhr.open("post","test.aspx",true);
xhr.send("123456789");
xhr.send("123456789");
+展开
if (Request.InputStream.Length > 0){
System.IO.StreamReader reader
= new System.IO.StreamReader(Request.InputStream);//注意如果必要时可以指定编码值,防止出现乱码
string v=reader.ReadToEnd();//值为123456789
reader.Close();
}
-C#
if (Request.InputStream.Length > 0){
System.IO.StreamReader reader
= new System.IO.StreamReader(Request.InputStream);//注意如果必要时可以指定编码值,防止出现乱码
string v=reader.ReadToEnd();//值为123456789
reader.Close();
}
asp
Function BytesToBstr(strBody,CodeBase) '需要将2进制流数据写入adodb.stream后生成字符串
dim obj
set obj=Server.CreateObject("Adodb.Stream")
obj.Type=1
obj.Mode=3
obj.Open
obj.Write strBody
obj.Position=0
obj.Type=2
obj.Charset=CodeBase
BytesToBstr=obj.ReadText
obj.Close
set obj=nothing
End Function
v=BytesToBStr(Request.BinaryRead (Request.TotalBytes),"gb2312")'注意编码
4)对于xhr提交方式为post,链接在url后的参数都可以使用Request.QueryString["键"]来获取
+展开
-JavaScript
xhr.open("post","test.aspx?m=MM",true);
xhr.setRequestHeader("content-type","application/x-www-url-encoded");
xhr.send("123456789");
xhr.setRequestHeader("content-type","application/x-www-url-encoded");
xhr.send("123456789");
+展开
-C#
string v=Request.Form.ToString();//得到123456789
string m=Request.QueryString["m"];//得到MM
string m=Request.QueryString["m"];//得到MM
asp
v=request.form
m=request.querystring("m")
综合测试例子test.aspx
+展开
-HTML
<%@ Page language="C#" ValidateRequest="false"%>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
System.Xml.XmlDocument doc = null;
if (Request.InputStream.Length > 0)//非 application/x-www-form-urlencoded类型
{
System.IO.StreamReader reader = new System.IO.StreamReader(Request.InputStream);
doc = new System.Xml.XmlDocument();
doc.LoadXml(reader.ReadToEnd());
reader.Close();
}
if (Request.Form.Count > 0)// application/x-www-form-urlencoded类型
{
doc = new System.Xml.XmlDocument();
doc.LoadXml(Server.UrlDecode(Request.Form.ToString()));
}
if (doc != null)
{
Response.Write(doc.GetElementsByTagName("item")[0].InnerText + "\n" + Request.QueryString["m"]);
Response.End();
}
}
</script>
<script type="text/javascript">
var xhr=new ActiveXObject("microsoft.xmlhttp");
xhr.open("post","alexa.aspx?m=MM",true)
//xhr.setRequestHeader("Content-Type", "text/plain");
//xhr.setRequestHeader("content-type","application/x-www-form-urlencoded")
xhr.send("<root><item>1</item></root>");
xhr.onreadystatechange=function(){
if(xhr.readyState==4)alert(xhr.responseText)
}
</script>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
System.Xml.XmlDocument doc = null;
if (Request.InputStream.Length > 0)//非 application/x-www-form-urlencoded类型
{
System.IO.StreamReader reader = new System.IO.StreamReader(Request.InputStream);
doc = new System.Xml.XmlDocument();
doc.LoadXml(reader.ReadToEnd());
reader.Close();
}
if (Request.Form.Count > 0)// application/x-www-form-urlencoded类型
{
doc = new System.Xml.XmlDocument();
doc.LoadXml(Server.UrlDecode(Request.Form.ToString()));
}
if (doc != null)
{
Response.Write(doc.GetElementsByTagName("item")[0].InnerText + "\n" + Request.QueryString["m"]);
Response.End();
}
}
</script>
<script type="text/javascript">
var xhr=new ActiveXObject("microsoft.xmlhttp");
xhr.open("post","alexa.aspx?m=MM",true)
//xhr.setRequestHeader("Content-Type", "text/plain");
//xhr.setRequestHeader("content-type","application/x-www-form-urlencoded")
xhr.send("<root><item>1</item></root>");
xhr.onreadystatechange=function(){
if(xhr.readyState==4)alert(xhr.responseText)
}
</script>
更多ajax问题请参考,ajax问题总结
类别:JavaScript/Ajax 作者:波波 日期:2010-03-02 【评论:0 阅读:】
暂时没有评论!
发表留言
同类热门博文
- ·AJAX跨域问题解决办..
- ·ajax+asp.net+mssql..
- ·ajax问题总结
- ·JavaScript解析XML的..
- ·JS URL编码函数
- ·ajax+asp+mssql无刷..
- ·ajax无刷新上传文件..
- ·美化alert,confirm..
博格Tag
- flash/flex/fcs/AIR(750)
- Asp.Net/C#/WCF(476)
- JavaScript/Ajax(232)
- 操作系统及应用软件(206)
- SQL及数据库(105)
- 黑客技术(96)
- Asp/VBScript(85)
- 网站排名及优化(82)
- PHP/apache/Perl(72)
- HTML/WML/CSS兼容(65)
- 其他(59)
- 个人日志(44)
- lucence.net/分词技术(33)
- C#设计模式(22)
- 计算机网络(17)
- 日语学习(15)
- Canvas/VML/SVG(13)
- linux(10)
- 游戏开发(8)
- 正则表达式(5)
- Jsp/Java(4)
最新博文
声明:本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载或引用的作品侵犯了您的权利,请通知我们,我们会及时删除!
Powered by showbo,G51人力资讯网,桂ICP备05005887号
Powered by showbo,G51人力资讯网,桂ICP备05005887号
