Examples of Ajax dynamic fetching JSON

Source: Internet
Author: User

1. Foreground script:

//Ajax for toggling the picture list        functionChangephoto (title,hotelid) {$.ajax ({contentType:"Application/x-www-form-urlencoded; Charset=utf-8 ", type:"POST", URL:"<@a.webroot/>/base/bms/hotel/hotelpicture-querypicturebytitleajax.action", data: {"HotelPicturePage.hotelPicture.title": Title,"HotelPicturePage.hotelPicture.hotelId": Hotelid}, DataType:"JSON", Success:function(data) {//Replace the contents of the small graph first                    varHotelpicturelist =data.hotelpciturelist; if(hotelpicturelist! = ' undefined ' && hotelpicturelist!=NULL&& Hotelpicturelist.length >0 ){                        varHtmlstr = ' <tr> ', Imgtitle = "", Imgurl = "";  for(vari = 0;i < hotelpicturelist.length;i++) {Imgtitle=Hotelpicturelist[i].title; Imgurl=Hotelpicturelist[i].imgurl; Htmlstr+ = ' <td originalphoto= ' ' +imgurl+ ' "></td> "; } htmlstr+ = ' </tr> '; $("#hotelPictureContainer"). HTML (HTMLSTR);//give the table new content$ ("#hotelPictureContainer"). Removeattr ("style");//clears the style created by the previous thumbnail slide                                                 //then replace the larger image with the first small image                        if(Hotelpicturelist[0].imgurl! =NULL&& hotelpicturelist[0].imgurl! = ' undefined ') {//determine if the small map exists$ ("#originalPhoto"). attr ("src", hotelpicturelist[0].imgurl); }                                                 //finally activate the click-to-replace large image of each small map$ ("#thumbContainer TD"). Click (function() {alert (22); varTdobj = $ ( This); Photoindex= $ ("#thumbContainer TD"). index (tdobj); $("#originalPhoto"). attr ("src", tdobj.attr ("Originalphoto"))); $("#photoContainer"). CSS ("width", ($ ("#originalPhoto"). Width ()) + "px");                                                 }); //update the data for the area of the small mapPhotocount = $ ("#thumbContainer img"). length;//Number of picturesLeftcount = Math.ceil (PHOTOCOUNT/5);//Number of pagesLeftlevel = 0;//Paging SeriesPhotoindex = 0;//Picture Number                     }                    Else{alert ("Sorry, the hotel is not related to the picture. "); }}, Error:function() {alert ("Data error, please try again later." ");        }            }); };
View Code

2. Background Java code:

 Public voidQuerypicturebytitleajax () {//First filter the page back to the title of "" Good "null" problem    if(Hotelpicturepage.gethotelpicture ()! =NULL ){        if("". Equals (Hotelpicturepage.gethotelpicture (). GetTitle ()) | | " Null. Equals (Hotelpicturepage.gethotelpicture (). GetTitle ())) {hotelpicturepage.gethotelpicture (). Settitle (NULL); }} jsonobject Jsobject=NewJsonobject (); //get the picture you needHotelpicturepage =Hotelpictureservice.queryhotelpicturebytitle (Commonpage, hotelpicturepage); if(hotelpicturepage.gethotelpicturelist () = =NULL|| Hotelpicturepage.gethotelpicturelist (). Size () <=0) {Jsobject.put ("Hotelpciturelist",NULL); }    Else{//Encapsulating JSON DataListhotelpicturepage.gethotelpicturelist (); List<JSONObject> jsonlist =NewArraylist<jsonobject>();  for(inti = 0;i < Hotelpciturelist.size (); i++) {Jsonobject Item=NewJsonobject (); Item.put ("Title", Hotelpciturelist.get (i). GetTitle ()); Item.put ("Imgurl", Hotelpciturelist.get (i). Getimgurl ());        Jsonlist.add (item); } jsobject.put ("Hotelpciturelist", jsonlist); }writejson (jsobject);}
View Code

Examples of Ajax dynamic fetching JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.