求救!关于ajax传递内容到action,数据不完整

凉岑玉 凉岑玉 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我通过fckeditor来编辑内容·然后通过ajax来发送请求给action,在ajax的函数里接收到的数据是包含有html标签的,当我发送到后台的action并打印出来的时候发现只打印了一个起始的<p>标签,存到数据库的时候也是只存储一个<p>标签·经仔细观察,发现只要编辑的数据中包含空格的话就必定会造成这种情况··百思不得其解··现贴出代码,忘高手们点拨一二··

AJAX的方法

function addComment() {
//以下是获取fckeditor的值
var fck=FCKeditorAPI.GetInstance("u_comment_a.commentContent").GetXHTML(true);
var userId=document.getElementById("userId").value;
var articleId=document.getElementById("articleId").value;

//fck=encodeURI(encodeURI(fck));//转码

alert("用户Id:"+userId);
alert("文章Id:"+articleId);
alert("评论内容:"+fck);

var xhr=new createXmlHttpRequest();//第一步,获取XHR对象
xhr.onreadystatechange=function (){//第二部,写监听函数
if (xhr.readyState == 4) {//条件判定
if (xhr.status == 200||xhr.status == 304) {
alert("评论成功!");
location.reload() ;
}
}
}

xhr.open("post","UserComment!addUsercommentarticle",true);//链接请求
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")//设置一下请求头
xhr.send("u_comment_a.commentContent="+fck+"&users.userId="+userId+"&article.articleId="+articleId);//发送
//return false;
}

action接收数据并处理的方法:

/**
* 添加一条评论的方法
* @return
*/
public void addUsercommentarticle() {
System.out.println(article.getArticleId()+"--------------------articleId--------------------");
System.out.println(users.getUserId()+"--------------------userId--------------------");
//解码
try {
u_comment_a.setCommentContent(java.net.URLDecoder.decode(u_comment_a.getCommentContent(), "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
System.out.println(u_comment_a.getCommentContent()+"--------------------u_comment_a------------------------");

try {
users = UserManagerBean.getUserById(users.getUserId());
article = ArticleManagerBean.getArticleById(article.getArticleId());
//每当评论一次就添加一次被评论的次数
article.setCommentCount(article.getCommentCount()+1);
//提交更新
ArticleManagerBean.updateArticle(article);
//再获取一次
// article = ArticleManagerBean.getArticleById(article.getArticleId());
System.out.println(article.getCommentCount()+"====================================");//打印评论次数看看
//设置评论所属文章及评论者
u_comment_a.setArticle(article);
u_comment_a.setUsers(users);
} catch (Exception e) {
e.printStackTrace();
}
UserCommentArticleManagerBean.addU_A(u_comment_a);
//return "commentSuccess";
}

以下是后台控制台打印出的结果

98--------------------articleId--------------------
9--------------------userId--------------------
<p>--------------------u_comment_a------------------------
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
凉岑玉
Java EE 67,535 成员 225,843 内容