返回Html代码的ajax函数(Data)是JSON

时间:2014-07-05 09:35:50

标签: ajax json

我使用MVC框架工作想从Db传递Emp id获取Employee名称。使用Ajax时返回

function getAjaxEmpName(){
  var jsonObject = [];
  jsonObject.push ({
  empNo: document.forms['empForm'].empNo.value
  });
 var jsonString = JSON.stringify(jsonObject);
  $.ajax({
    type:"POST",
    url:"./empDetails.do?",
    data: "state=GetEmpName" + "&jsonInput=" + jsonString,
    success : function(data, textStatus, jqXHR) {
        alert("output is :"+data);
        var empNameJson = JSON.parse(data);
        var empName = empNameJson.empName;
        if($.trim(empName) != '') {
           alert($.trim(empName));
        } else {
           document.forms['empForm'].submit();
        }
        },
 timeout: 5000,
 error: function(jqXHR, textStatus, errorThrown) {
 if (textStatus.indexOf("error") >= 0) {
 alert("Failed")
 }
 }
});
}

    private void runGetEmpNameState(ControllerRequest request, ControllerResponse response)throws ControllerException,TPReplyException {
String empName="";
try {
    if (log.isDebugEnabled()) log.debug("+++++++ getempName+++++");
    HttpServletRequest req = (HttpServletRequest)((ServletControllerRequest) request).getServletRequest();
    HttpSession ses = req.getSession();
    SessionDO sessionDO = SessionHelper.testAndSetDO(ses);
    String jsonResultString = "";
    String json = (String) request.getParameter(ActionConst.JSON_INPUT_PARAM);
    log.debug("json = "+ json);
    JSONArray jsonArray = JSONArray.fromObject(json);
    JSONObject jsonObject = (JSONObject) jsonArray.get(0);
    String empNo = jsonObject.getString("empNo");
    SL svcLocator = SL.getInstance();
    log.debug("Emp No is : " +empNo );
    log.debug("****userID in *****"+userProfile.getUserId());    
    empData empmgr = (empData) svcLocator.getEmpDataHandle();
    empName = empmgr.getempName(EmpNo, userProfile);
    if (log.isDebugEnabled()) log.debug("empName from the server is :"+empName);
    if(empName != null && empName.length()>0){
    JSONObject jsonOutput = new JSONObject();
    jsonOutput.put("empName", empName);
    log.debug("jsonOutput.toString() is  = " + jsonOutput.toString());
    sessionDO.setJsonOutput(jsonOutput.toString());
}
}catch (Exception e)
  {
   log.error("Exception: caught in runGetEmpNameState:  "+Utils.getStackTrace(e));
   throw new ControllerException(myName + " Exception: caught in runGetEmpNameState "+ e.getMessage());
}
}

后端输出:

2014-07-04 07:40:51,257 DEBUG [13] (empCtrl.java:runGetEmpNameState:2005) - +++++++ GetEmpName +++++
2014-07-04 07:40:51,257 DEBUG [13] (empCtrl.java:runGetEmpNameState:2015) - json = [{"empNo":"1234"}]
2014-07-04 07:40:51,257 DEBUG [13] (empCtrl.java:runGetEmpNameState:2037) - *** EMPNo  is : 1234
2014-07-04 07:40:51,258 DEBUG [13] (empCtrl.java:runGetEmpNameState:2039) - ****userID in *****abc_user12
2014-07-04 07:40:51,713 DEBUG [13] (empCtrl.java:runGetEmpNameState:2047) - empName  from the server is :Raja
2014-07-04 07:40:51,714 DEBUG [13] (empCtrl.java:runGetEmpNameState:2059) - After setting the jsonOutput = Raja
2014-07-04 07:40:51,714 DEBUG [13] (empCtrl.java:runGetEmpNameState:2060) - jsonOutput.toString() is  = {"empName":"Raja"}

数据中的JSP输出显示整个JSP调用,如果是JSON字符串({“empName”:“Raja”})。

output is : <!DOCTYPE HTML PUBLIC ">
<html> 
<head>
<link rel="stylesheet" href="styles.css" type="text/css" media="screen">
<link rel="stylesheet" href="content.css" type="text/css" media="screen">
<link rel="stylesheet" href="jquery-ui-1.10.4.custom.min.css" type="text/css" media="screen">   
<link rel="stylesheet" href="print.css" type="text/css" media="print">
<title>Emp Data</title>   
<script language="javascript" type="text/javascript" src="jquery-1.7.2.js"></script>
<script language="javascript" type="text/javascript" src="jquery-ui-1.10.4.custom.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript">
    function getAjaxEmpName(){
          var jsonObject = [];
      jsonObject.push ({
        empNo: document.forms['empForm'].empNo.value
    });
    var jsonString = JSON.stringify(jsonObject);
      $.ajax({
        type:"POST",
        url:"./empDetails.do?",
        data: "state=GetEmpName" + "&jsonInput=" + jsonString,
        success : function(data, textStatus, jqXHR) {
            alert("output is :"+data);   
            var empNameJson = JSON.parse(data);
            var empName = empNameJson.empName;
            if($.trim(empName) != '') {
                alert($.trim(empName));
            } else {
                document.forms['empForm'].submit();
            }
        },
        timeout: 5000,
        error: function(jqXHR, textStatus, errorThrown) {
            if (textStatus.indexOf("error") >= 0) {
                alert("Failed")
              }
             }
         });
        }
    </head>
    </html>

请提供解决此问题的方法。提前谢谢。

0 个答案:

没有答案