返回Html代码的ajax函数(数据)是JSON
我使用MVC框架希望从数据库中获取员工姓名,并传递Emp id返回Html代码的ajax函数(数据)是JSON,ajax,json,Ajax,Json,我使用MVC框架希望从数据库中获取员工姓名,并传递Emp id function getAjaxEmpName(){ var jsonObject = []; jsonObject.push ({ empNo: document.forms['empForm'].empNo.value }); var jsonString = JSON.stringify(jsonObject); $.ajax({ type:"POST", url:"./empDetail
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"}
如果JSON字符串{empName:Raja},数据中的JSP输出将显示整个JSP调用
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>
请提供解决此问题的方法。提前感谢。我不能100%肯定我理解您的问题,但听起来您使用的是java Spring MVC,而不是ajax调用返回一个简单的JSON编码对象,而是返回整个视图。对吗?如果是这样,我们需要查看ajax请求的控制器empDetails.do1。您必须以有效的JSON格式输出数据。2.我建议您将数据的内容/类型设置为application/json 3。您是否尝试过ajax方法的数据类型参数?您好,John,您的理解完全正确。@Blauharley如果在ajax中添加dataType:json,则调用该请求,而不是调用服务器。