ajax指定为post但是未设置content-type或未指定键时时如何获取提交的内容
今天在CSDN看到一个ajax指定了提交方式为post,但是设置了content-type为text/plain的时,如何在提交的页面获取提交值的问题。由于content-type为text/plain,动态页并未帮你处理成键值对的形式,所以你得自己使用2进制流数据生成对应的string类型的数据。
1)要生成键值对形式,你得指定content-type为“application/x-www-form-urlencoded”
xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");//注意要在open后才能调用setRequestHeader
这样就可以在动态页使用
asp
v=Request.Form("键")
来获取对应的键值。
2)在发送数据时未指定键,则可以使用Request.Form.ToString()获取提交的内容
xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
xhr.send("123456789");
asp
v=request.form
3)未设置content-type或者content-type设置成非application/x-www-form-urlencoded,则需要读取2进制流数据
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();
}
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["键"]来获取
xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
xhr.send("123456789");
string m=Request.QueryString["m"];//得到MM
asp
v=request.form
m=request.querystring("m")
综合测试例子test.aspx
<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 + " " + 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("
xhr.onreadystatechange=function(){
if(xhr.readyState==4)alert(xhr.responseText)
}
</script>
更多ajax问题请参考,ajax问题总结
- pro
日期:2011-6-17 10:17:06 IP:59.61.*.*
謝謝,太有幫助了。
但是我讀取流的時候,Request.InputStream,提示readerstream“此流不支持超時”,怎麼解決。管理员回复(2011-6-17 11:15:37)
你怎么用的啊~~是按照我的那个示例代码吗?
我测试过没有“此流不支持超时”的错误。
- pro
日期:2011-6-17 17:58:42 IP:59.61.*.*
MSXML2.XMLHTTPClass objH = new MSXML2.XMLHTTPClass();
objH.open("Post", url, true, Type.Missing, Type.Missing);
objH.setRequestHeader("Content-Type ", "application/x-www-form-urlencoded");
objH.setRequestHeader("SOAPAction ", url);
objH.send(str);
我就是这样发生的。
但是我讀取流的時候,Request.InputStream,提示readerstream“此流不支持超時”,怎麼解決。管理员回复(2011-6-18 11:27:20)
你是怎么接收的?
因该没有这种错误啊,是不是你加了其他判断导致错误。
下面这句改成同步的试试,有可能是异步导致的问题+展开-C#//objH.open("Post", url, true, Type.Missing, Type.Missing);
objH.open("Post", url, false, Type.Missing, Type.Missing);
- ·AJAX跨域问题解决办..
- ·ajax问题总结
- ·jQuery dataType指定..
- ·ajax+asp.net+mssql..
- ·ajax无刷新上传文件..
- ·ajax对象abort方法
- ·JavaScript代码,变..
- ·fckeditor编辑器在F..
- flash/flex/fcs/AIR(752)
- Asp.Net/C#/WCF(595)
- 操作系统及应用软件(376)
- JavaScript/Ajax(330)
- SQL及数据库(134)
- 黑客技术(115)
- Asp/VBScript(111)
- HTML/WML/CSS兼容/XML(102)
- PHP/apache/Perl(96)
- 网站排名及优化(92)
- 其他(75)
- 个人日志(66)
- lucene.net/分词技术(33)
- 计算机网络(26)
- 机械重工(26)
- C#设计模式(24)
- Google Maps开发(17)
- 日语学习(15)
- Canvas/VML/SVG(13)
- linux(11)
- 游戏开发(8)
- 正则表达式(5)
- Jsp/Java(4)
- ·javascript实现html..
- ·Javascript风格要素..
- ·Javascript风格要素
- ·动态加载JavaScript
- ·JavaScript陷阱
- ·ajax技巧
- ·IE对CSS样式表的限制..
- ·什么是Javascript匿..
