【JavaWeb】jQuery对Ajax的支持
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>
【JavaWeb】jQuery对Ajax的支持的更多相关文章
- IT兄弟连 JavaWeb教程 jQuery对AJAX的支持
jQuery对AJAX的支持 jQuery对Ajax请求的创建.发送.响应.注册数据处理函数.JSON的解析和缓存以及传参等都进行了相应的封装,同时也考虑了浏览器的兼容性问题. jQuery中对AJA ...
- Unit02: JSON 、 使用JSON实现数据交换 、 jQuery对AJAX的支持,编码问题
Unit02: JSON . 使用JSON实现数据交换 . jQuery对AJAX的支持 1. 编码问题 (1)发送get请求 为什么会产生乱码? ie浏览器提供的ajax对象,对中文会使用gbk来编 ...
- jQuery九大选择器和jQuery对ajax的支持
一.jQuery九大选择器 1)基本选择器: <body> <div id="div1ID">div1</div> <div id=&qu ...
- IT兄弟连 JavaWeb教程 jQuery对AJAX的支持经典面试题
1.简述对Ajax的理解 AJAX是Asynchronous JavaScript and Xml异步的JavaScript和Xml.它一种用来改善用户体验的技术其实质是使用XMLHttpReques ...
- IT兄弟连 JavaWeb教程 jQuery对AJAX的支持经典案例
案例需求:编写用户登陆页面的验证码模块,在用户进行登陆时,输入验证码后不需要点击提交按钮,使用AJAX异步地向服务器发送验证验证码的请求.如果验证码正确,可以点击提交按钮,如果验证码输入错误,提示用户 ...
- jQuery对ajax的支持
有load,get,post和ajax四个方法都可以,其中后三个直接使用$.xx调用 使用方法大致相同,这里只介绍get和post方法 -------------------------------- ...
- jquery ajax中支持哪些返回类型以及js中判断一个类型常用的方法?
1 jquery ajax中支持哪些返回类型在JQuery中,AJAX有三种实现方式:$.ajax() , $.post , $.get(). 预期服务器返回的数据类型.如果不指定,jQuery 将自 ...
- jquery.form.js官方插件介绍Form插件,支持Ajax,支持Ajax文件上传
jquery.form.js官方插件介绍Form插件,支持Ajax,支持Ajax文件上传 http://www.malsup.com/jquery/form/#getting-started [JQu ...
- 【JAVAWEB学习笔记】28_jquery加强:json数据结构、jquery的ajax操作和表单校验插件
Ajax-jqueryAjax 今天内容: 1.json数据结构(重点) 2.jquery的ajax操作(重点) 3.jquery的插件使用 一.json数据结构 1.什么是json JSON(Jav ...
随机推荐
- ubuntu安装gcc不同的版本
服务器ubuntu14.04安装ns3.29中,显示gcc版本过低 使用apt-get安装失败,ubuntu14.04默认安装gcc4.8.4,无法下载更高级的gcc版本 先找到资料1,脚本尝试了,下 ...
- Ubuntu Server中安装keepalived
场景 Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Kee ...
- FCC---Create Texture by Adding a Subtle Pattern as a Background Image
One way to add texture and interest to a background and have it stand out more is to add a subtle pa ...
- Dynamics 365应用程序池回收对连接造成的影响。
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- PHP时间戳相互转换
1.获取当前时间方法date()很简单,这就是获取时间的方法,格式为:date(format,format,timestamp),format为格式.timestamp为时间戳–可填参数. 2.获取时 ...
- RabbitMQ入门及其几种工作模式
1.简介 MQ全程Message Queue,用于应用程序和应用程序间进行通信.RabbitMQ采用Erlang编写,实现了AMQP(高级消息队列)协议,跨平台,支持各种主流的操作系统和多种客户端. ...
- All 432 functions were compiled because no usable IPDB/IOBJ from previous compilation was found
解决方法: 将: 链接器——>优化——>链接时间代码生成——>使用快速链接时间代码生成(/LTCG:incremental). 改为: 链接器——>优化——>链接时间代码 ...
- Leetcode 216. 组合总和 III
地址 https://leetcode-cn.com/problems/combination-sum-iii/ 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并 ...
- P4728 [HNOI2009]双递增序列
题意 这个DP状态有点神. 首先考虑一个最暴力的状态:\(f_{i,j,k,u}\)表示第一个选了\(i\)个,第二个选了\(j\)个,第一个结尾为\(k\),第二个结尾为\(u\)是否可行. 现在考 ...
- 利用Python几行代码批量生成验证码
几行代码批量生成authCode 整体步骤: 1.创建图片 2.创建画笔 3.在图片上生成点 4.在图片上画线 5.在图片在画圆 6.在图片在写文本 7.在图片在生成指定字体的文本 代码奉上 #!/u ...