1. 程式人生 > >使用Jquery的Ajax例項

使用Jquery的Ajax例項

$('#changId).change(function(){

$.ajax({

type: 'POST',

data:"id="+$('#id).val(),

url: 'whms.html?method=getType',

success: function(json){

var jsont = eval(json);

var row = "<option value='-1'>Please Select</option>";

$('#selectId').append(row);

$('# selectId').append(row);

for(i=0;i<jsont.length;i++){

json=jsont[i];

var id=json[0];

var name=json[1];

var row = "<option value="+id+">"+name+"</option>";

$('# selectId').append(row);

}

},

error:function(data){

alert("error!");

}

})

});

//這裡是控制器的部分

@RequestMapping(params = " method=getType")

publicvoidgetType (Model model, HttpServletRequest request, HttpServletResponse response)

throws IOException {

Long id = request.getParamater("id");

ArrayList<Object> list = whmsManager.getType(id);

JSONArray json = JSONArray.fromObject(list);

PrintWriter out = null;

out = response.getWriter();

out.print(json.toString());

}

//這裡是業務邏輯的部分

public ArrayList<Object> getType (long id) {

ArrayList<Object> al = new ArrayList<Object>();

List<Whms> list = getMaterialType(Id);

for (Type type: list) {

ArrayList<String> innerList = new ArrayList<String>();

innerList.add(type.getId().toString());

innerList.add(type.getName());

innerList.add(type.getWhmsTypeName());

al.add(innerList);

}

return al;

}

:不能傳入一個已經定義好的實體,因為JSONArray.fromObject,所以建議使用dwr