function publishTopic(){
var subjectId = $("#subjectId").attr("value");
var topicContent = $("#topicContent").val();
if (topicContent==null || typeof(topicContent)=="undefined" || topicContent==""){
alert("评论内容不能为空!!!");
}
$.ajax({
url: "/topic/publishTopic.action",
type: "POST",
data: "topicAbstractInfoVO.belongSubjectId="+subjectId+"&r="+ (new Date()).getTime(),
dataType: "json",
async:true,
success: function(reMsg){
alert(reMsg);
console.log("111"+reMsg);
var a=0
var b=1;
b=a+b;
alert("111111111");
/*if(reMsg.success==true){
window.location.href = "/index.action?topicAbstractInfoVO.belongSubjectId="+subjectId+"&r=" + (new Date()).getTime();
}else{
var msg = reMsg.msg;
alert(msg);
}*/
},
error:function(reMsg){
alert(reMsg);
console.log("222"+reMsg);
var a=0
var b=1;
b=a+b;
alert("22222222222");
// alert("链接服务器异常!!!");
}
});
}
ajax请求方法如上所示。
1. 出现的问题:
当发起请求以后,服务器端还没有执行完毕,前台js就进入到error的处理程序,弹出222222222的对话框。
2. 查找问题:
2.1 检查ajax语法、struts配置、action处理逻辑,都没有发现问题。
(1)ajax语法: success、error配置没有问题;url、date参数没有问题;type为json也没有问题
(2)struts配置返回的类型为json的格式,同样的配置和其他正常的配置一样,没有发现问题
(3)action返回值为亦为json序列化的数据,符合json协议的规范
2.2 检查页面元素:
(1)页面元素内容:<a href="" class="n-right" onclick="publishTopic();">发布</a>
(2) 之前调试时,发现如果使用a标签,链接跳转会有问题,不是不跳转就是只在本页蹦跶
(3) 将a标签的href属性设置为“#”,试下下,竟然正常了
3. 解决方法:
<a href="#" class="n-right" onclick="publishTopic();">发布</a>
在使用a标签,并且添加onclick事件时,需要将href的属性设置为#号;否则就有问题