日期:2014-05-20  浏览次数:20703 次

jquery+ajax返回List问题
JScript code

jQuery.noConflict();   
jQuery(document).ready(function () {
     /*
     * 加载名单返回类型json—list
     * @param {Object} data
     */
    var url = "aa.action";
    var params;    
    jQuery.getJSON(url,params,function callback(data){
        alert("2");//返回不了
      jQuery.each(data.items,function(i,item){
        jQuery("#maindiv").append(item.cname);
    });//这句有没有问题,我参照jquery+ajax帮助文档写的
    })
});


HTML code

<div id="maindiv"></div>       


Java code

package com.tyut.actions;
import java.util.List;
import net.sf.json.JSONArray;
import com.opensymphony.xwork2.Action;
import com.tyut.domain.Candiate;
import com.tyut.service.CandiateService;

public class CandiateAction {

    private Candiate candiate;
    
    public Candiate getCandiate() {
        return candiate;
    }
    
    private JSONArray resulttype;

    
    public JSONArray getResulttype() {
        return resulttype;
    }

    public void setResulttype(JSONArray resulttype) {
        this.resulttype = resulttype;
    }

    public void setCandiate(Candiate candiate) {
        this.candiate = candiate;
    }
        
    public String aa(){
        CandiateService candiateService = new CandiateService();
        List<Candiate> candiateList = candiateService.getList();

        /*for (Iterator iter = candiateList.iterator(); iter.hasNext();) {  

           Candiate candiate = (Candiate) iter.next();  
           System.out.println(candiate.getCname());

          }  */
        JSONArray jsarry=new JSONArray();  
        jsarry=JSONArray.fromObject(candiateList);  
        
        resulttype=jsarry;     
        return Action.SUCCESS;
    }
}



XML code

        <package name="aa" namespace="/" extends="json-default">
        <action name="aa" class="com.tyut.actions.CandiateAction" method="aa">
           <result type="json">
              <param name="root">resulttype</param>
           </result>
        </action>
    </package>



------解决方案--------------------
你这个是数组又不是键值对

item[0] -> ["1","姚林志"]
item[0][1] -> "姚林志"
------解决方案--------------------
循环each....
------解决方案--------------------
sf的json包我没用过,自己翻下他的文档,看那个函数是以对象形式构建json串的

我用的是官方json包
JSONArray ary = new JSONArray(List);
------解决方案--------------------
大哥,你返回的是二维数组呀,当然要用数组的取法,如果你要使用obj.property这种取法的话,你server端返回的数据格式就应该是[{"cid": 1, "cname": "kotori"},...]
------解决方案--------------------
Candiate这个类没实现get函数吧
你可以翻一下jsonobject的代码,是查找以get开头的函数来编织json map表的
------解决方案--------------------
顶起,我也再看这个,不过不太懂,弄好了可否给我一份烟酒烟酒