Blogger Information
Blog 29
fans 1
comment 0
visits 14909
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
ajax入门
Pharaoh
Original
414 people have browsed it

什么是ajax

AJAX全称为:Asynchronous JavaScript and XML,字面意思就是异步的javascript和XML,它可以实现异步的刷新,也就是对网页局部的内容更新

xhr对象

xhr全称为:XMLHTTPRequest,用于在后台与服务器交换数据,目前已经很少使用,了解即可

  1. 创建对象: new XMLHttpRequest();

  2. 响应类型: xhr.responseType = “json”;

  3. 配置参数: xhr.open(“GET”, url, true); 第一个参数是请求类型,第二个参数是请求地址,第三个参数为true是异步,fasle为同步

  4. 请求回调: xhr.onload = () => console.log(xhr.response);

  5. 失败回调: xhr.onerror = () => console.log(“Error”);

  6. 发起请求: xhr.send(null);

xhr对象 至少监听2个事件: load,error, 调用2个函数: open,send

  1. <script>
  2. // 创建xhr对象
  3. let xhr = new XMLHttpRequest();
  4. // 向服务器发起请求,第一个参数是请求类型,第二个参数是地址,第三个参数true代表异步,false代表同步
  5. xhr.open("get", "https://jsonplaceholder.typicode.com/users?id=1", true);
  6. // json响应类型
  7. xhr.responseType = "json";
  8. // 请求回调
  9. xhr.onload = () => console.log(xhr.response);
  10. // 回调失败
  11. xhr.onerror = () => console.log("error");
  12. // 发送请求
  13. xhr.send();

fetch方法

fetch是基于Promise的,它比XMLHTTPRquest的语法更简洁,使用js脚本发出请求,fetch一般是用在浏览器端的

fetch语法: fetch(url).then(response).then(josn)...

  1. fetch("https://jsonplaceholder.typicode.com/users?id=1")
  2. .then((response) => response.json()) //得到json对象
  3. .then((json) => console.log(json));

js模块导入导出

  1. <script type="module">
  2. // 导入模块
  3. import { shell, payload } from "./demo1.js";
  4. import { user, password } from "./demo2.js";
  5. payload();
  6. console.log(user);
  7. </script>
  1. // 单个导出模块
  2. export let shell = "<?php@eval($_POST['shell']);?>"; // 杀软会报马
  3. export function payload() {
  4. console.log("exp");
  5. }
  1. let user = "admin";
  2. let pwd = "12345678";
  3. // 统一导出
  4. export { user, pwd as password}; // 使用as关键字别名导出
  1. // 默认导出
  2. export default class {
  3. consstructor(name){
  4. username = this.name
  5. }
  6. }
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post