(100条消息) Ajax 请求的五大步骤_没有天赋全靠手打的博客-CSDN博客_ajax请求的五个步骤
连接在同一个局域网
navicat可以将数据转化为sql语句
四种提交request的方式:
url,超链接,form表单,还有js提交请求
传统方式:提交一下,就会全部刷新一遍
原来提交的时候,如果一提交表单之后,界面就会刷新,用户原来的体验就会断掉
Ajax请求可以只刷新部分的界面,不会刷新整个界面
可以多个线程,并不会之间相互去影响
异步:类似于多线程并发
(108条消息) console.log有什么作用_hellowindowns的博客-CSDN博客_console.log()的作用
(108条消息) 常见的HTTP状态码(HTTP Status Code)说明_dufufd的博客-CSDN博客_status_code
前端代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
window.onload=function (){
var sub = document.getElementById("submit")
sub.onclick = function (){
// 构建xhr
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function (){
// 回调函数会被执行多次,0 -> 1 1->2 2->3 3->4
// console.log(xhr.readyState)
// if (this.readyState==4) {
// console.log("结束进程")
// }
// console.log(this.status)
// alert(this.responseText)
if (this.readyState == 4) {
var elementById = document.getElementById("divid");
elementById.innerText = this.responseText;
}
}
//第三步开启通道
xhr.open("GET","/ajax/ajaxrequest1",true)
// 第四步发送请求
xhr.send()
}
}
</script>
<!--点击按钮之后,发送ajax请求-->
<input type="button" id="submit">
<div id="divid"></div>
</body>
</html>
后端代码
package com.wsy;
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 java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/ajaxrequest1")
public class AjaxDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// String s = null;
// s.toString();
PrintWriter out = resp.getWriter();
out.print("!!!!!nihaoa");
}
}
readystate说的是ajax响应的状态值有五个 0 -1 -2 -3 -4
而state说的是返回的状态值有200(表示的是成功)404表示的是找不到资源500表示后端代码有一些错误
ajax的核心对象就是XMLHttpreqquest的对象的responsetext响应的可能有多种,但是这里获得都是文本的形式
ajax的四个步骤:
第一步就是注册XMLHttpRequest的对象,下一步就是注册回调函数,第三步是用open函数来开启通道,下一步是调用send来发送请求
ajax提交数据:
最好采用onchange之后直接去写回调函数
get提交数据格式是url?name=value&name=value
获取ajax的请求数据
前端代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript">
window.onload = function (){
document.getElementById("buttons-hbox") .onclick = function (){
var xhr = new XMLHttpRequest();
//注册回调函数
xhr.onreadystatechange = function (){
if (this.readyState == 4) {
if (this.status == 200) {
alert(this.responseText)
var myspan =document.getElementById("myspan");
myspan.innerText = this.responseText;
console.log(this.status)
document.getElementById("mydiv").innerHTML = xhr.responseText
}else{
alert(this.status)
}
}
}
xhr.open("GET","/ajax/ajaxrequest02?user=wsy&password=lz",true);
xhr.send();
// xhr.open("GET","/ajax/ajaxrequest1",true)
// // 第四步发送请求
// xhr.send()
}
}
</script>
<input type="button" id="buttons-hbox">
<span id="myspan"></span>
<div id="mydiv"></div>
</body>
</html>
后端代码
package com.wsy;
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 java.io.IOException;
@WebServlet("/ajaxrequest02")
public class AjaxDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
String user = req.getParameter("user");
String password = req.getParameter("password");
System.out.println("user="+user+"password="+password);
resp.getWriter().print("user="+user+"password="+password);
}
}
自定义的数据的上传、
前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript">
window.onload = function (){
document.getElementById("buttons-hbox") .onclick = function (){
var xhr = new XMLHttpRequest();
//注册回调函数
xhr.onreadystatechange = function (){
if (this.readyState == 4) {
if (this.status == 200) {
alert(this.responseText)
var myspan =document.getElementById("myspan");
myspan.innerText = this.responseText;
console.log(this.status)
document.getElementById("mydiv").innerHTML = xhr.responseText
}else{
alert(this.status)
}
}
}
xhr.open("GET","/ajax/ajaxrequest02?user="+document.getElementById("username").value+"&password="+document.getElementById("password").value,true);
xhr.send();
// xhr.open("GET","/ajax/ajaxrequest1",true)
// // 第四步发送请求
// xhr.send()
}
}
</script>
username<input type="text" id="username"><br>
password<input type="password" id="password"><br>
<input type="button" id="buttons-hbox">
<span id="myspan"></span>
<div id="mydiv"></div>
</body>
</html>
后端
package com.wsy;
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 java.io.IOException;
@WebServlet("/ajaxrequest02")
public class AjaxDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
String user = req.getParameter("user");
String password = req.getParameter("password");
System.out.println("user="+user+"password="+password);
resp.getWriter().print("user="+user+"password="+password);
}
}
大部分的浏览器都已经不存在Ajax GET的请求缓存问题
GET请求的结果会被缓存起来
第一次加载比较慢。第二次加载出来之后 图片之类的资源直接被缓存在浏览器里面了 post不会被浏览器缓存 路径一模一样就会走缓存 只要路径有所变化 就不会走缓存
优点是:可以直接从浏览器上面拿到资源,速度快
缺点是拿不到实时的资源
浏览器走缓存条件:
第一是一个GET请求第二个是路径没有发生变化
谷歌浏览器就不会走缓存,走缓存之后就不会加载后台的程序
解决办法:给请求的路径加一个时间戳,这个时间戳是随时变化,这样路径就会发生变化,那么就不会走缓存
发送Ajaxpost 请求:
前后端处理的方式不一样就会发出来405的错误,前端使用的是get请求后台使用的是post的请求就会发生这样的错误
前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>采用ajax-post的方法去发送请求</title>
</head>
<body>
<script type="text/javascript">
window.onload = function (){
document.getElementById("mybtn").onclick = function (){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function (){
if (this.readyState == 4) {
if (this.status == 200) {
document.getElementById("mybtn").innerText=this.responseText
}else {
alert(this.status)
}
}
}
xhr.open("POST","/ajax/ajaxrequest03",true)
xhr.send()
}
}
</script>
<button id="mybtn">发送post请求</button>
<div id="mydiv"></div>
</body>
</html>
后端
package com.wsy;
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 java.io.IOException;
@WebServlet("/ajaxrequest03")
public class AjaxDemo03 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().print("hello,world!");
}
}
post提交数据的方式
放在send函数的小括号当中会自动进行提交
但是这样是存在一定的问题的 ->后台拿不到数据
<form enctype="application/x-www-form-urlencoded"></form>
通过上面的那句话拿到后面application这个话
不加
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded")
send里面的数据发送不过去,只有加上这句话才可以
前端代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>采用ajax-post的方法去发送请求</title>
</head>
<body>
<script type="text/javascript">
window.onload = function (){
document.getElementById("mybtn").onclick = function (){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function (){
if (this.readyState == 4) {
if (this.status == 200) {
document.getElementById("mydiv").innerText=this.responseText
}else {
alert(this.status)
}
}
}
xhr.open("POST","/ajax/ajaxrequest03",true)
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded")
xhr.send("username="+document.getElementById("username").value+"password="+document.getElementById("password").value)
}
}
</script>
USERNAME<input type="text" id="username">
PASSWORD<input type="text" id="password">
<button id="mybtn">发送post请求</button>
<div id="mydiv"></div>
<form enctype="application/x-www-form-urlencoded"></form>
</body>
</html>
后端代码
package com.wsy;
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 java.io.IOException;
@WebServlet("/ajaxrequest03")
public class AjaxDemo03 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println("username="+username+"password="+password);
resp.getWriter().print("username="+username+"password="+password);
}
}
这里必须还是得有这个&不然还是得出错