Struts2+Jquery Ajax 总是返回error

Cute_Tiger 2013-03-27 05:31:55

public class JsonAction extends EmpBaseAction{
private EmpDto empDto;
private String loc;
HttpServletRequest request;
public String findLoc() throws IOException, SQLException{
request = ServletActionContext.getRequest();
String deptno = request.getParameter("deptno");
System.out.println("deptno-----------------------------------------------"+deptno);
loc = EmpDtoDao.findLocByDeptno(deptno);
System.out.println("loc-----------------------------------------------"+loc);
empDto.setLoc(loc);
return "success";
}

public EmpDto getEmpDto() {
return empDto;
}
public void setEmpDto(EmpDto empDto) {
this.empDto = empDto;
}
}

调试代码都能打印出来。

struts.xml代码:


<package name="json" extends="json-default" namespace="/emp">
<action name="jsonAction" class="com.sisj.action.JsonAction" method="findLoc">
<result name="success" type="json">
<param name="contentType">
text/html
</param>
<param name="includeProperties">
empDto\.loc
</param>
</result>
</action>
</package>



JS代码:


<script type="text/javascript" >
$(function(){
$("#deptno").change(function(){
var deptno = $('#deptno').val();

$.ajax({
type: "POST",
url: "jsonAction.action",
data: {"deptno": deptno, "method": "findLoc"},
dataType: 'json',
// async: true,
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("获取失败。");
alert("http请求的状态:"+XMLHttpRequest.status);//200
alert("XMLHttpRequest对象的处理状态:"+XMLHttpRequest.readyState);//4
alert(textStatus);//parsererror
},
success: function(data){
alert("SUCCESS");
if(data.empDto.loc == undefined){
$('label:#locId').empty();
}else{
$('label:#locId').html(data.empDto.loc);
}
}
});

});
});
</script>
...全文
199 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Cute_Tiger 2013-03-28
  • 打赏
  • 举报
回复
引用 1 楼 showbo 的回复:
$(function(){
    $("#deptno").change(function(){
        var deptno = $('#deptno').val();
 
         $.ajax({
            type: "POST",
            url: "jsonAction.action",
            data: {"deptno": deptno, "method": "findLoc"},
            dataType: 'json',
           // async: true,
             error: function(XMLHttpRequest, textStatus, errorThrown) {
                 alert("获取失败。");
                alert("http请求的状态:"+XMLHttpRequest.status);//200
                alert("XMLHttpRequest对象的处理状态:"+XMLHttpRequest.readyState);//4
                alert(textStatus);//parsererror
alert(XMLHttpRequest.responseText)////////////看返回什么内容
            },
            success: function(data){
                alert("SUCCESS");
                if(data.empDto.loc == undefined){
                    $('label:#locId').empty();
                }else{
                    $('label:#locId').html(data.empDto.loc);
                }
            }
        });
 
    });
});
通过alert(textStatus);定位到错误所在了,empDto.setLoc(loc)空指针了,private EmpDto empDto;改成 private EmpDto empDto= new EmpDto();就好了,谢谢版主锅锅。
Go 旅城通票 2013-03-27
  • 打赏
  • 举报
回复
$(function(){
    $("#deptno").change(function(){
        var deptno = $('#deptno').val();
 
         $.ajax({
            type: "POST",
            url: "jsonAction.action",
            data: {"deptno": deptno, "method": "findLoc"},
            dataType: 'json',
           // async: true,
             error: function(XMLHttpRequest, textStatus, errorThrown) {
                 alert("获取失败。");
                alert("http请求的状态:"+XMLHttpRequest.status);//200
                alert("XMLHttpRequest对象的处理状态:"+XMLHttpRequest.readyState);//4
                alert(textStatus);//parsererror
alert(XMLHttpRequest.responseText)////////////看返回什么内容
            },
            success: function(data){
                alert("SUCCESS");
                if(data.empDto.loc == undefined){
                    $('label:#locId').empty();
                }else{
                    $('label:#locId').html(data.empDto.loc);
                }
            }
        });
 
    });
});

52,797

社区成员

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

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