ajax的post传参数 没反应

xsjinfeng 2010-07-13 07:38:08
我想把一段html代码传到后台

<script language="javascript" type="text/javascript" src="ajax.js"></script>
<script language="javascript" type="text/javascript">
function ss(){

sendAJAX("Default3.aspx","cont=<div>ss</div>","POST",onComplate,true);
}

function onComplate(res){
alert(res);
}
</script>

可是含有html标签的参数没办法传
救命啊















var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}

function sendAJAX(url,pars,method,onComplete,asynchronous,resType)
{
createXMLHttpRequest();
//默认用get提交
if(method.toLowerCase() == "get")
{
if(pars==""){
url = url+"&time=" +new Date().getTime();//加入此参数的原因为解决缓存问题
}else{
url = url+"?"+pars+"&time=" +new Date().getTime();//加入此参数的原因为解决缓存问题
}

xmlHttp.open("GET",url,asynchronous);
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status==200)
{
//此处可对应更改为xmlHttp.responseXML和xmlHttp.responseStream
if(resType=="xml"){
onComplete(xmlHttp.ResponseXML);
}
else{
onComplete(xmlHttp.responseText);
}

}
}
}
xmlHttp.send(null);
}
else {

if(pars==""){
url = url+"?time=" +new Date().getTime();//加入此参数的原因为解决缓存问题
}else{
url = url+"?"+pars+"&time=" +new Date().getTime();//加入此参数的原因为解决缓存问题
}


xmlHttp.open("POST",url,asynchronous);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(pars);

xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status==200)
{
//此处可对应更改为xmlHttp.responseXML和xmlHttp.responseStream
if(resType=="xml"){
onComplete(xmlHttp.responseXML);
}
else{
onComplete(xmlHttp.responseText);
}
}
}
}
}
}
...全文
343 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yudoggy 2010-07-13
  • 打赏
  • 举报
回复
将这段字符串html encode一下,然后再提交,在服务器端用Server.HTMLDecode得到值
javascript的html encode函数自己写,大体是:

将"&"替换为"&"(这个在最前面)
将"<"替换为"<"
将">"替换为">"
将 "(双引号)替换为"""

Go 旅城通票 2010-07-13
  • 打赏
  • 举报
回复
提交到aspx页面的数据如果含有html标签的话,需要给page指令加ValidateRequest="false" ,要不会出错

<%@ Page Language="C#"  ValidateRequest="false" %>

52,797

社区成员

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

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