才学着用jquery的ajax,用json封装对象在action中找不到,求大神帮看下:
js:
[code="java"]
$("#editViewMethod").click(function(){
var ids = "";
var flag = 0;
var jsonStr = "";
$("input[type='checkbox'][name!='checkAll']").each(function(){
if($(this).attr("checked")){
flag = flag + 1;
ids = ids+","+$(this).attr("value");
}
});
if(flag == 0){
alert("您没有选择!");
}else if(flag > 1){
alert("请选择一条记录!");
}else{
alert("ids="+ids);
jsonStr = '["0","5"]';
//alert(jsonStr);
//$.post("delServerMethod.do",{"idList":jsonStr}, delMethod,"json");
var para = {"idList":"[\"0\",\"5\"]"};
var para1 = {"methods.methodId":"5","methods.methodName":"1"};
var p = jQuery.parseJSON(para1);
$.ajax({
type: "POST",
url: "json/editServerMethod.do",
data: para1,
dateType:"json",
error: function(){
alert("Error loading json Object");
},
success: function(data){
alert(data);
//$.each(json,function(i,n){
// alert("Key:"+i);
// alert("Value:"+n);
//});
//var jsonMsg = JSON.stringify(json);
//alert(jsonMsg);
alert(data.methods.methodId);
}
});
}
});
[/code]
上面是js部分
[code="java"]
<package name="ajaxJson" namespace="/soms_sys/cdn/serverManager/json" extends="json-default" >
<action name="delServerMethod" method="delServerMethod" class="com.jwzt.soms.cdn.action.CdnServMethodAct">
<result name="del_success" type="json">
<param name="includeProperties">message</param>
</result>
</action>
<action name="editServerMethod" method="editServerMethod" class="com.jwzt.soms.cdn.action.CdnServMethodAct">
<result name="edit_success" type="json">
<param name="includeProperties">methods\.*</param>
<param name="excludeProperties">methods\.soms4CdnServer</param>
</result>
</action>
</package>
[/code]
[code="java"]
@SuppressWarnings("serial")
public class CdnServMethodAct extends JeeCoreAction
{
protected static Logger logger = LoggerFactory.getLogger(CdnServerAct.class);
private Soms4CdnAccessMethods methods;
private List<String> idList;
private String ids;
private String message;
public String delServerMethod(){
String id = idList.get(0);//methods.getMethodId();
System.out.println("get Method Id from ajax json:"+id);
this.setMessage("success");
System.out.println("json return =="+this.getMessage());
idList = new ArrayList<String>();
idList.add("1");
idList.add("2");
return "del_success";
}
public String editServerMethod(){
String id = idList.get(0);//methods.getMethodId();
System.out.println("get Method Id from ajax json:"+id);
this.setMessage("success");
System.out.println("json return =="+this.getMessage());
idList = new ArrayList<String>();
idList.add("1");
idList.add("2");
methods.setMethodId(5);
methods.setMethodName("test");
return "edit_success";
}
public Soms4CdnAccessMethods getMethods()
{
return methods;
}
public void setMethods(Soms4CdnAccessMethods methods)
{
System.out.println("进来了,我是JSON...............");
System.out.println("Soms4CdnAccessMethods...............");
System.out.println("methods.name="+methods.getMethodName());
this.methods = methods;
}
public List<String> getIdList()
{
return idList;
}
public void setIdList(List<String> idList)
{
System.out.println("进来了,我是JSON...............");
System.out.println("list长度==="+idList.size());
System.out.println("idList[0]="+idList.get(0));
this.idList = idList;
}
//setter or getter
[/code]
死活出不来
这个包:
commons-collections-3.2.jar
commons-lang-2.3.jar
commons-logging-1.0.4.jar
ezmorph-1.0.3.jar
json-lib-2.1-jdk15.jar
struts2-json-plugin-2.2.1.jar
[code="java"]
2010-12-30 10:51 WARN c.j.c.h.HibernateSessionFilter - HibernateSessionFilter begin doChain
进来了,我是JSON...............
Soms4CdnAccessMethods...............
methods.name=null
[/code]
证明是能进action的 但前面ajax总是error
data用para也能进action,但idList总是第一个有值
[code="java"]
2010-12-30 10:56 WARN c.j.c.h.HibernateSessionFilter - HibernateSessionFilter begin doChain
进来了,我是JSON...............
list长度===1
idList[0]=["0","5"]
get Method Id from ajax json:["0","5"]
json return ==success
[/code]
这是怎么回事?
向各位大神求教..
急 急 急