刚接触ajax和json,遇到点小问题,半天都没解决,希望大侠们支援哪。。。
感激万分~~
ajax调用action顺利执行了,result里有了内容,但页面弹框显示为null或者直接不显示
import org.json.JSONObject;
//result、username的setter、getter方法
public String showUser(){
List list = userDAO.findByUsername("king");
System.out.println("userame"+userame);
JSONObject obj=new JSONObject();
try {
obj.put("users", list);
result=obj.toString();
System.out.println("result: "+result);
//显示:result: {"users":[{"userpassword":"king","username":"king","userid":1}]}
} catch (JSONException e) {
e.printStackTrace();
}
return Action.NONE;
//return SUCCESS;//换成这句js不能弹窗
}
Action成功调用,username接收正常,
struts2.1.6:
<package name="userpk" extends="json-default" >
<action name="user" class="userAction">
<result type="json" name="result">
</result>
<!-- <result type="json">
<param name="root">result</param>
</result> -->
</action>
</package>
jsp页面js:
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
$.ajax({
type: "POST",
url: "user!showUser",
dateType: "json",
data: {"userame":"wang"},
success: function(data,status){
//var json = eval( "("+result+")" );
//alert("json"+json);
alert("result:"+data);
alert("status:"+status);
}
});
});
});
</script>
------解决方案--------------------
浏览器直接访问user!showUser看输出什么内容。
------解决方案--------------------
import org.json.JSONObject;
//result、username的setter、getter方法
public String showUser(){
List list = userDAO.findByUsername("king");
System.out.println("userame"+userame);
JSONObject obj=new JSONObject();
try {
obj.put("users", list);
result=obj.toString();
System.out.println("result: "+result);
//显示:result: {"users":[{"userpassword":"king","username":"king","userid":1}]}