前台:
var quickNumber = "test";
try{
var url="xxx!method.action";
$.ajax({
url:url,
type : "post",
dataType : "json",
cache : false,
data:{quickNumber:quickNumber},
success : function(data) {
if(data.indexOf("error:") == 0){
var msg = data.replace("error:","");
alert("获取单号信息失败 !原因如下:" + msg);
} else {
var info = eval(data);
alert(info.length);
}
},
error:function(){}
});
}catch(e){
alert(e);
}
public String test(){
JSONArray infos = new JSONArray();
for(int i=0;i<2;i++){
JSONObject info = new JSONObject();
info.put("val","111");
info.put("kkk","222");
infos.add(info);
}
System.out.println(infos.toString());
message = JSONObject.toJSONString(infos);;
return "ajax_success";
}
其中,JSONObject是用的是
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
这两个
Struts.xml里配置
<!-- wen-bin,快捷窗口查询--> <action name="xxx" class="xxx" method="test"> <result name="ajax_success" type="json"> <param name="root">message</param> </result> </action>