json服务器端

    /**
* 根据品牌id动态加载标的物
*
@return
*
@throws DataBaseException
*
@throws IOException
*/
public String loadModel() throws DataBaseException, IOException{
HttpServletRequest request
=ServletActionContext.getRequest();
HttpServletResponse response
=ServletActionContext.getResponse();

response.setContentType(
"application/x-javascript;charset=UTF-8");
PrintWriter out
= response.getWriter();

String brandId
= request.getParameter("brandId");
List phones
= this.phoneService.queryMobilByBrandId(Integer.parseInt(brandId));
if(phones!=null&&phones.size()>0){
//把集合对象转换为json对象
JSONArray json=JSONArray.fromObject(phones);
out.println(json.toString());
out.close();
}
else{
out.println(
"[{id:0,model:'请选择'}]");
out.close();
}
return SUCCESS;
}

            

   

             

function loadPhone(value){
alert(value);
$.ajax({
type: "post",
url: "supply!loadPhone.do",
data: ({brandId: value}),
success: function(data){
var objs=eval("("+data+")");
var $phone=$("#phone");
$phone.empty();
//$phone.append("
<option value='0'>..请选择..</option>");
for(var i=0;i
<objs.length;i++){
$phone.append("<option value
='"+objs[i].id+"'>"+objs[i].model+"</option>");
}
}
});
}


        

   

<select id="brand" name="brandId" onchange="loadPhone(this.options[this.options.selectedIndex].value)">

你可能感兴趣的:(json)