1、前台脚本:

//用于切换图片列表的ajax
function changePhoto(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) {
//先将小图的内容替换
var hotelPictureList = data.hotelPcitureList;
if(hotelPictureList !='undefined' && hotelPictureList!= null && hotelPictureList.length >0 ){
var htmlStr = '<tr>', imgTitle = "", imgUrl = "";
for(var i = 0;i < hotelPictureList.length;i++){
imgTitle = hotelPictureList[i].title;
imgUrl = hotelPictureList[i].imgUrl;
htmlStr += '<td originalPhoto="'+imgUrl+'"><img src="'+imgUrl+'" title="'+imgTitle+'" height="75" alt="'+imgTitle+'" width="100" name="imgUrl"></td>';
}
htmlStr += '</tr>';
$("#hotelPictureContainer").html(htmlStr);//赋予table新的内容
$("#hotelPictureContainer").removeAttr("style");//清除之前小图滑动产生的样式 //然后使用第一张小图替换大图
if(hotelPictureList[0].imgUrl != null && hotelPictureList[0].imgUrl != 'undefined'){//判断下小图是否存在
$("#originalPhoto").attr("src",hotelPictureList[0].imgUrl);
} //最后激活各个小图的点击替换大图
$("#thumbContainer td").click(function(){
alert(22);
var tdObj = $(this);
photoIndex = $("#thumbContainer td").index(tdObj);
$("#originalPhoto").attr("src",tdObj.attr("originalPhoto"));
$("#photoContainer").css("width",($("#originalPhoto").width())+"px");
}); //更新小图的区的数据
photoCount = $("#thumbContainer img").length;//图片数量
leftCount = Math.ceil(photoCount / 5);//分页数量
leftLevel = 0;//分页级数
photoIndex = 0;//图片序号 }
else{
alert("抱歉,该酒店暂无相关图片。");
}
},
error : function(){
alert("数据错误,请稍后再试。");
}
});
};

2、后台java代码:

public void queryPictureByTitleAjax(){
//先过滤下页面传回的Title为""好"null"的问题
if(hotelPicturePage.getHotelPicture() != null ){
if("".equals(hotelPicturePage.getHotelPicture().getTitle()) || "null".equals(hotelPicturePage.getHotelPicture().getTitle())){
hotelPicturePage.getHotelPicture().setTitle(null);
}
}
JSONObject jsobject = new JSONObject();
//获取需要的图片
hotelPicturePage = hotelPictureService.queryHotelPictureByTitle(commonPage, hotelPicturePage);
if (hotelPicturePage.getHotelPictureList() == null||hotelPicturePage.getHotelPictureList().size()<=0) {
jsobject.put("hotelPcitureList", null);
}
else {//封装json数据
List<HotelPicture> hotelPcitureList = hotelPicturePage.getHotelPictureList();
List<JSONObject> jsonList = new ArrayList<JSONObject>();
for(int i = 0;i < hotelPcitureList.size();i++){
JSONObject item = new JSONObject();
item.put("title", hotelPcitureList.get(i).getTitle());
item.put("imgUrl", hotelPcitureList.get(i).getImgUrl());
jsonList.add(item);
}
jsobject.put("hotelPcitureList", jsonList);
}
writeJson(jsobject);
}

ajax 动态获取json的例子的更多相关文章

  1. ECharts 环形饼图 动态获取json数据

    ECharts  环形饼图 动态获取json数据 效果图如下: 一.html部分 <div id="secondPieChart" style="width:100 ...

  2. jquery通过ajax方法获取json数据不执行success

    1.jquery通过ajax方法获取json数据不执行success回调 问题描述:jquery通过ajax方法获取json数据不执行success回调方法 问题原因:json格式存在问题或不符合标准 ...

  3. jquery通过ajax方法获取json数据不执行success回调

    问题描述:jquery通过ajax方法获取json数据不执行success回调方法 问题原因:json格式存在问题或不符合标准写法,导致总是执行error回调方法 解决方案:使json格式务必符合下述 ...

  4. echarts通过ajax动态获取数据的方法

    echarts表格的数据一般都需要动态获取,所以总结了一下通过ajax动态获取数据的操作: 插入的方法应该不止一种,我也是接触不久,所以刚学会了一种插入方法: 灵感和经验来自:https://www. ...

  5. 用ajax动态获取数据显示在highcharts上

    html代码(index.html) <html><head> <meta charset="UTF-8" /> <title>Hi ...

  6. ajax模拟获取json

    现在工作中我用到获取数据的方式,基本都是ajax.前台获取后端的数据后,需要进行处理,然后把他们放进页面中的相应标签里.下面举一个简单的例子,来模拟数据的获取和摆放. 这里用ng框架获取数据然后处理, ...

  7. 发送Ajax请求获取JSON格式数据

    Aspx前端页面: <script type="text/javascript"> $(function () { $.getJSON("Ajax/TestA ...

  8. 利用autocomplete.js实现仿百度搜索效果(ajax动态获取后端[C#]数据)

    实现功能描述: 1.实现搜索框的智能提示 2.第二次浏览器缓存结果 3.实现仿百度搜索 <!DOCTYPE html> <html xmlns="http://www.w3 ...

  9. jquery select 列表 ajax 动态获取数据 模糊查询 分页

    最近需要一个这样的select 在网上找的多是数据一次性获取到再通过前端模糊查询匹配的 这样在数据量比较大的情况下不适合 ,所以参考http://www.jq22.com/jquery-info145 ...

随机推荐

  1. MVC4 经典增删改查详情demo

    MVC4 经典增删改查详情demo 源码 不解释 Mvc4增删改查详情Demo.7z public ActionResult Detail(int? id)  {    ViewData.Model ...

  2. Linux 性能监测:介绍

    看了某某教程.读了某某手册,按照要求改改某某设置.系统设定.内核参数就认为做到系统优化的想法很傻很天真:)系统优化是一项复杂.繁琐.长期的 工作,优化前需要监测.采集.测试.评估,优化后也需要测试.采 ...

  3. SQL数据库约束行为---防止数据乱填(即数据规范化)

    防止乱填:一.Check约束.按照某种规则对数据进行检查.操作:在表的设计界面中,右击相应的列,选择“CHECK约束”在弹出的对话框中,设置约束的名称和表达式. 代码实现: create table ...

  4. 重定向redirect与跳转forward区别

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoResponse.a ...

  5. 判断List、Map、Set是否为空及效率比较

      //如果object为null,则设置为defaultValue ObjectUtils.defaultIfNull(object, defaultValue); //判断集合是否为null Li ...

  6. [Gym]2008-2009 ACM-ICPC, NEERC, Moscow Subregional Contest

    比赛链接:http://codeforces.com/gym/100861 A模拟,注意两个特殊的缩写. #include <bits/stdc++.h> using namespace ...

  7. SAP、BW 权限控制设置

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  8. [数据结构与算法]栈Stack的多种实现

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. hdu 2199 (二分)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2199 Can you solve this equation? Time Limit: 2000/1000 ...

  10. 读Effective Java笔记之one:static Factory methods instead of Constructors (静态工厂方与构造器)

    获取类的实例的方法有很多种,在这很多种方法中,它们各有优缺,各有特点.这里,只介绍2中方法 1.使用构造方法 public class Person { private String sex; /** ...