
批改狀態(tài):合格
老師批語:
AJAX全稱為:Asynchronous JavaScript and XML,字面意思就是異步的javascript和XML,它可以實(shí)現(xiàn)異步的刷新,也就是對網(wǎng)頁局部的內(nèi)容更新
xhr全稱為:XMLHTTPRequest,用于在后臺與服務(wù)器交換數(shù)據(jù),目前已經(jīng)很少使用,了解即可
創(chuàng)建對象: new XMLHttpRequest();
響應(yīng)類型: xhr.responseType = “json”;
配置參數(shù): xhr.open(“GET”, url, true); 第一個參數(shù)是請求類型,第二個參數(shù)是請求地址,第三個參數(shù)為true是異步,fasle為同步
請求回調(diào): xhr.onload = () => console.log(xhr.response);
失敗回調(diào): xhr.onerror = () => console.log(“Error”);
發(fā)起請求: xhr.send(null);
xhr對象 至少監(jiān)聽2個事件: load,error, 調(diào)用2個函數(shù): open,send
<script>
// 創(chuàng)建xhr對象
let xhr = new XMLHttpRequest();
// 向服務(wù)器發(fā)起請求,第一個參數(shù)是請求類型,第二個參數(shù)是地址,第三個參數(shù)true代表異步,false代表同步
xhr.open("get", "https://jsonplaceholder.typicode.com/users?id=1", true);
// json響應(yīng)類型
xhr.responseType = "json";
// 請求回調(diào)
xhr.onload = () => console.log(xhr.response);
// 回調(diào)失敗
xhr.onerror = () => console.log("error");
// 發(fā)送請求
xhr.send();
fetch是基于Promise的,它比XMLHTTPRquest的語法更簡潔,使用js腳本發(fā)出請求,fetch一般是用在瀏覽器端的
fetch語法:
fetch(url).then(response).then(josn)...
fetch("https://jsonplaceholder.typicode.com/users?id=1")
.then((response) => response.json()) //得到j(luò)son對象
.then((json) => console.log(json));
<script type="module">
// 導(dǎo)入模塊
import { shell, payload } from "./demo1.js";
import { user, password } from "./demo2.js";
payload();
console.log(user);
</script>
// 單個導(dǎo)出模塊
export let shell = "<?php@eval($_POST['shell']);?>"; // 殺軟會報(bào)馬
export function payload() {
console.log("exp");
}
let user = "admin";
let pwd = "12345678";
// 統(tǒng)一導(dǎo)出
export { user, pwd as password}; // 使用as關(guān)鍵字別名導(dǎo)出
// 默認(rèn)導(dǎo)出
export default class {
consstructor(name){
username = this.name
}
}
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號