jQuery对Ajax的支持

  • jQuery对Ajax进行封装,提供了$.ajax()方法
  • 语法:$.ajax(options)
常用设置项 说明
url 发送请求地址
type 请求类型get|post
data 向服务器传递的参数
dataType 服务器响应的数据类型
text|json|xml|html|jsonp|script
success 接收响应时的处理函数
error 请求失败时的处理函数

实例代码

MusicServlet.java

package demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson.JSON; /**
* Servlet implementation class MusicSetvlet
*/
@WebServlet("/music")
public class MusicServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public MusicServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String songType = request.getParameter("songType");
System.out.println(songType);
List<String> song = new ArrayList<>();
if(songType.equals("流行歌曲")) {
song.add("稻香");
song.add("晴天");
song.add("告白气球");
}else if(songType.equals("经典歌曲")) {
song.add("千千阙歌");
song.add("傻女");
song.add("七友");
}else if(songType.equals("摇滚歌曲")) {
song.add("一块红布");
song.add("假行僧");
song.add("星长征路上的摇滚");
}
String json = JSON.toJSONString(song);
response.setContentType("text/html;charset=utf-8");
response.getWriter().println(json);
} }

musicList.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
<style>
div {
text-align: center;
} .mystyle {
width: 30%;
cursor: pointer;
}
</style>
</head>
<body>
<div>
<input class="mystyle" type="button" value="流行歌曲"> <input
class="mystyle" type="button" value="经典歌曲"> <input
class="mystyle" type="button" value="摇滚歌曲">
</div>
<div id="divContent"></div>
<script type="text/javascript" src="js/jquery-3.4.1.js"></script>
<script type="text/javascript">
$(":button").click(function() {
var songType = this.value;
$(function() {
$.ajax({
"url" : "/ajax/music",
"type" : "get",
"data" : {
"songType" : songType
},
"dataType" : "json",
"success" : function(json) {
$("#divContent>span").remove();
$("#divContent>br").remove();
for (var i = 0; i < json.length; i++) {
var html = "<span>" + json[i] + "</span><br>";
$("#divContent").append(html);
}
},
"error" : function(xmlhttp, errorText){
console.log(xmlhttp);
console.log(errorText);
if(xmlhttp.status == "405"){
alert("无效的请求方式");
}else if(xmlhttp.status == "404"){
alert("未找到URL资源");
}else if(xmlhttp.status == "500"){
alert("服务器内部错误,请联系管理员");
}else{
alert("产生异常,请联系管理员");
}
}
})
})
})
</script>
</body>
</html>

最新文章

  1. ios开发中的小技巧
  2. Python3的tkinter写一个简单的小程序
  3. asp的gridview
  4. Gabor学习笔记
  5. bootstrap插件学习-bootstrap.popover.js
  6. JAVA 新闻
  7. 阻止Application_End事件的解决方案
  8. python(6)
  9. SDC(3)&ndash;set_multicycle_path 最关键的一张图
  10. More on 1Password’s Components
  11. Spring Timer 两种实现
  12. Basic脚本解释器移植到STM32
  13. androidpn-server笔记及BUG修改
  14. Cobalt Strike 服务器搭建及使用
  15. 剑指offer编程题Java实现——面试题14调整数组顺序使奇数位于偶数之前
  16. SSM(Spring+SpringMVC+Mybstis)搭建,写给新手
  17. POI 3.17
  18. ZooKeeper安装方法具体解释
  19. Ubuntu 14.04开启ssh服务
  20. 【转】python assert用法

热门文章

  1. Java开发人员必备十大工具
  2. IT 常用的网址
  3. mysql登陆时出现ERROR 2013 (HY000): Lost connection to MySQL server at &#39;reading initial communication packet&#39;, system error: 0
  4. 微信小程序官方文档中表单组建button部分有关function(type)中type的个人理解
  5. scp 拷贝 针对软连接的问题
  6. Python爬虫(学习准备)
  7. DOS下查看驱动版本号
  8. 第04组 Beta冲刺(3/5)
  9. The trap of Bash trap
  10. 时间time()和$_SERVER[&#39;REQUEST_TIME&#39;]