$.ajax({
// 请求方式为get或者post等
type: "GET",
// 服务器响应的数据类型
dataType: "json",
// 请求的url(一般为后台接口)
url: "test.json",
// 发送到服务器的参数,
data: {user:"xiao"},
// 请求成功后返回的数据,赋值给变量'data'
success: function(data){
// 对data进行操作
},
// 请求失败异常后,返回的错误信息
error: function(err){
}
});
$.get("route/findAllRoute",{"cid":cid,"curentPage":curentPage,"pageCount":pageCount},function (data) {
writepage(data);
})
$.post("route/findTheRoute",{"rid":rid},function (data) {
if(data.flag){
$(".titleCategory:eq(0)").html(data.data.category.cname);
$(".titleRoute:eq(0)").html(data.data.routeIntroduce);
$("#bigPic").attr("src",data.data.routeImgList[0].bigPic);
var littleImg='';
for(var i=0;i<data.data.routeImgList.length;i++){
if(i>3){
littleImg+='+data.data.routeImgList[i].bigPic+'" style="display:none;">\n' +
'+data.data.routeImgList[i].smallPic+'">\n' +
'';
}else {
littleImg+='+data.data.routeImgList[i].bigPic+'">\n' +
'+data.data.routeImgList[i].smallPic+'">\n' +
'';
}
}
littleImg+='';
$("#littlePics").html(littleImg);
$(".titleCategory:eq(1)").html(data.data.rname);
$(".titleRoute:eq(1)").html(data.data.routeIntroduce);
/*经营商家 :中国国旅
咨询电话 : 400-000-1111
地址 : xxxxxxxx
*/
$("#sname").html("经营商家 : "+data.data.seller.sname);
$("#sphone").html("咨询电话 : "+data.data.seller.consphone);
$("#address").html("地址 : "+data.data.seller.address);
$("#price").html(data.data.price);
$("#count").html("已收藏"+data.data.count+"次");
loadImgs();
var timer=setInterval(auto_play,2000);
}
});
载入远程 HTML 文件代码并插入至 DOM 中。
url (String) : 请求的HTML页的URL地址。
data (Map) : (可选参数) 发送至服务器的 key/value 数据。
callback (Callback) : (可选参数) 请求完成时(不需要是success的)的回调函数。
这个方法默认使用 GET 方式来传递的,如果[data]参数有传递数据进去,就会自动转换为POST方式的。jQuery 1.2 中,可以指定选择符,来筛选载入的 HTML 文档,DOM 中将仅插入筛选出的 HTML 代码。语法形如 “url #some > selector”。
这个方法可以很方便的动态加载一些HTML文件,例如表单。
$(".ajax.load").load("http://www.cnblogs.com/yeer/archive/2009/06/10/1500682.html .post",
function (responseText, textStatus, XMLHttpRequest){
this;//在这里this指向的是当前的DOM对象,即$(".ajax.load")[0]
//alert(responseText);//请求返回的内容
//alert(textStatus);//请求状态:success,error
//alert(XMLHttpRequest);//XMLHttpRequest对象
});
格式:var data = $(“form”).serialize();
功能:将表单内容序列化成一个字符串。
这样在ajax提交表单数据时,就不用一一列举出每一个参数。只需将data参数设置为 $(“form”).serialize() 即可
$("#btLogin").click(function () {
$.post("user/login",$("#loginForm").serialize(),function (data) {
console.log($("#loginForm").serialize());
if (data.flag) {
//跳转到首页
location.href = "index.html";
}else{
$("#errorMsg").text(data.errorMsg);
}
});
});
console.log($("#loginForm").serialize());
结果:
action=login&username=aaaaaaa&password=hhhhhhhhh&check=a2d4
格式:var jsonData = $(“form”).serializeArray();
功能:将页面表单序列化成一个JSON结构的对象。注意不是JSON字符串。
比如,[{“name”:“lihui”, “age”:“20”},{…}] .
public class BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
//获取URI---> user/login
String reqURI = req.getRequestURI();
//获取请求的方法名--->将URI从最后一次'/'出现的位置切割,提取方法名login
String method = reqURI.substring(reqURI.lastIndexOf('/')+1);
// System.out.println(method);
try {
Class<? extends BaseServlet> aClass = this.getClass();
//得到子类Sevlet字节码文件,用字节码文件提取子类servlet的所有方法数组
Method[] methods = aClass.getMethods();
//遍历数组
for (Method m:methods) {
// System.out.println(m.getName());
//找到请求的方法名为method的方法
if(m.getName().equals(method)){
//调用方法,this就是子类Servlet对象
// m.invoke(servlet,req,resp);
m.invoke(this,req,resp);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
package com.dbxy.travel.web.servlet;
import com.bdqn.travel.pojo.Category;
import com.bdqn.travel.pojo.PageRoute;
import com.bdqn.travel.pojo.ResultInfo;
import com.bdqn.travel.pojo.Route;
import com.dbxy.travel.service.CategoryService;
import com.dbxy.travel.service.CategoryServiceImpl;
import com.dbxy.travel.service.RouteService;
import com.dbxy.travel.service.RouteServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/route/*")
public class RouteServlet extends BaseServlet{
private RouteService service=new RouteServiceImpl();
public void findAllRoute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String nowpage = request.getParameter("curentPage");
String cid=request.getParameter("cid");
PageRoute<Route> allRoute = service.findAllRoute(Integer.parseInt(nowpage), Integer.parseInt(cid));
returnResult(allRoute,response);
}
public void findTheRoute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String rid=request.getParameter("rid");
ResultInfo info = service.findRouteByRid(Integer.parseInt(rid));
returnResult(info,response);
}
public void findRouteBySearchName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String nowpage = request.getParameter("curentPage");
String search= request.getParameter("search");
String decodeSearch = null;
try {
System.out.println(nowpage);
decodeSearch = URLDecoder.decode(search, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// System.out.println("now"+nowpage);
PageRoute<Route> pageRoute=service.findRouteBySearchName(decodeSearch,Integer.parseInt(nowpage));
returnResult(pageRoute,response);
}
public void findRouteBySearchNameAndCid(HttpServletRequest request, HttpServletResponse response){
String nowpage = request.getParameter("curentPage");
String search= request.getParameter("search");
String cid = request.getParameter("cid");
String decodeSearch = null;
try {
System.out.println(nowpage);
decodeSearch = URLDecoder.decode(search, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
PageRoute<Route> pr=service.findRouteBySearchNameAndCid(Integer.parseInt(nowpage),decodeSearch,Integer.parseInt(cid));
returnResult(pr,response);
}
public void findFavoriteRoutes(HttpServletRequest request, HttpServletResponse response){
String nowpage = request.getParameter("curentPage");
PageRoute<Route> favoriteRoutes = service.findFavoriteRoutes(Integer.parseInt(nowpage));
returnResult(favoriteRoutes,response);
// System.out.println(favoriteRoutes);
}
public void favoriteSearch(HttpServletRequest request, HttpServletResponse response){
String search= request.getParameter("searchName");
String nowpage= request.getParameter("curentPage");
String decodeSearch = "";
try {
decodeSearch = URLDecoder.decode(search, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String minPrice= request.getParameter("minPrice");
String maxPrice= request.getParameter("maxPrice");
PageRoute<Route> pr =service.favoriteSearch(Integer.parseInt(nowpage),decodeSearch,minPrice,maxPrice);
returnResult(pr,response);
}
}