首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

Struts2+Jquery Ajax 老是返回error

2013-04-02 
Struts2+Jquery Ajax总是返回errorpublic class JsonAction extends EmpBaseAction{private EmpDto empDto

Struts2+Jquery Ajax 总是返回error


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>


[解决办法]
$(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);
                }
            }
        });
 
    });
});

热点排行