json 서버측

5308 단어 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)">

좋은 웹페이지 즐겨찾기