我用post请求发送数据给服务器,但是相应的功能不好使,后台也不报错//创建部门
function createDept(){
$.ajax({
url : "../login/createDept.action",
type : 'POST',
data:{jsonStr:JSON.stringify({deptName:$("#cdeptName").val(),deptDesc:$("#cdeptDesc").val()})},
contentType : "application/json",
dataType : 'json',
timeout : 5000,
//async:false,
cache : false,
success : function(data) {
if (data=="true") {
reset();
closeBg();
showDept();
}else{
alert("创建部门失败");
}
}
});
}strust.xml中
<action name="createDept" class="deptAction" method="createDept">
<result type="json" name="success">
<param name="root">state</param>
</result>
<result type="json" name="error">
<param name="root">returnJson</param>
</result>
</action>
function createDept(){
$.ajax({
url : "../login/createDept.action",
type : 'POST',
data:{jsonStr:JSON.stringify({deptName:$("#cdeptName").val(),deptDesc:$("#cdeptDesc").val()})},
contentType : "application/json",
dataType : 'json',
timeout : 5000,
//async:false,
cache : false,
success : function(data) {
if (data=="true") {
reset();
closeBg();
showDept();
}else{
alert("创建部门失败");
}
}
});
}strust.xml中
<action name="createDept" class="deptAction" method="createDept">
<result type="json" name="success">
<param name="root">state</param>
</result>
<result type="json" name="error">
<param name="root">returnJson</param>
</result>
</action>
如果你的命名空间是login的话,那么直接用login/createDept.action,没有命名空间就用createDept.action。给你一个找错误的思路:
1. 先判断错误在哪步,debug调试看看请求有没有到deptAction中
2. 调试看看createDept有没有出错
3. IE8 debug调试js部分。。不会debug就通过alert(data);看看有没有正确返回数据这几步就能解决问题了
如果是的话你的web.xml里面配置*.action的映射没? 没的话就把.action去掉。
你的strut.xml里面写package了没? 写的话就加上这个package的名字+action,即login/createdpt。直接就这样写,别的写多了反而没用!
url : "login/createDept",
"${pageContext.request.contextPath }/login/createDept.action"