【分享+散分】ajax指定为post但是未设置content-type时如何获取提交的内容

Go 旅城通票 2010-03-02 10:45:49
加精
文章来源:http://www.coding123.net/blogdetail1768.html

  今天在CSDN看到一个ajax指定了提交方式为post,但是设置了content-type为text/plain的时,如何在提交的页面获取提交值的问题。由于content-type为text/plain,动态页并未帮你处理成键值对的形式,所以你得自己使用2进制流数据生成对应的string类型的数据。

希望对有需要的朋友有帮助。

  1)要生成键值对形式,你得指定content-type为“application/x-www-url-encoded”
xhr.open("post","动态页",true);
xhr.setRequestHeader("content-type","application/x-www-url-encoded");//注意要在open后才能调用setRequestHeader



  这样就可以在动态页使用下面的代码来获取对应的键值。
string v=Request.Form["键"];




  

2)在发送数据时未指定键,则可以使用Request.Form.ToString()获取提交的内容
xhr.open("post","test.aspx",true);
xhr.setRequestHeader("content-type","application/x-www-url-encoded");
xhr.send("123456789");



string v=Request.Form.ToString();//得到123456789






3)未设置content-type或者content-type设置成非application/x-www-url-encoded,则需要读取2进制流数据
xhr.open("post","test.aspx",true);
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();
}


 

 4)对于xhr提交方式为post,链接在url后的参数都可以使用Request.QueryString["键"]来获取
xhr.open("post","test.aspx?m=MM",true);
xhr.setRequestHeader("content-type","application/x-www-url-encoded");
xhr.send("123456789");


string v=Request.Form.ToString();//得到123456789
string m=Request.QueryString["m"];//得到MM






  综合测试例子test.aspx
<%@ 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>


  



更多ajax问题请参考,ajax问题总结
...全文
4175 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Go 旅城通票 2010-03-04
  • 打赏
  • 举报
回复
~~~~~~~~~~~up~~~~~~~~~~
hailang7210 2010-03-03
  • 打赏
  • 举报
回复
地对地导弹地对地导弹的点对点地对地导弹
cs5276 2010-03-02
  • 打赏
  • 举报
回复
学习 + 收藏,谢谢分享
ws_hgo 2010-03-02
  • 打赏
  • 举报
回复
学习
Thanks!!
阿非 2010-03-02
  • 打赏
  • 举报
回复
温故而知新,up~
asdujiayong 2010-03-02
  • 打赏
  • 举报
回复
抢座位先

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧