分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

Ajax

发布时间:2023-09-06 02:14责任编辑:彭小芳关键词:Ajax

Ajax创建与响应

var xhr = new XMLHttpRequest();//创建Ajax// readyState获取状态的值console.log(xhr.readyState);//=>0 初始化请求代理对象xhr.open("GET","test.php");//请求地址console.log(xhr.readyState);//=>1 open()被调用后,建立一个与服务端的连接xhr.send();//发送请求// onreadystatechange状态发生改变就执行该事件xhr.addEventListener("readystatechange",function(){ ???if(this.readyState == 2){ ???????// =>2 可以获取响应报文的响应头 ???????// getAllResponseHeaders()响应头 ???????console.log(this.getAllResponseHeaders()); ???????// date: Thu, 13 Sep 2018 01:04:51 GMT ???????// server: Apache/2.4.34 (Win64) OpenSSL/1.0.2o PHP/7.2.9 ???????// connection: Keep-Alive ???????// x-powered-by: PHP/7.2.9 ???????// content-length: 9 ???????// keep-alive: timeout=5, max=45 ???????// content-type: text/html; charset=UTF-8 ???????console.log(this.getResponseHeader("date"));//获取单个响应头 ???}else if(this.readyState == 3){ ???????// =>3 正在下载响应报文的响应体 ???}else if(this.readyState == 4){ ???????// =>4 已经接收到了响应报文的响应体 ???????// responseText 响应体 ???????console.log(this.responseText); ???}});

onload()

var xhr = new XMLHttpRequest();xhr.open("GET","test.php");xhr.send();xhr.onload = function(){//HTML5的事件.低版本不支持 ???console.log(this.readyState); ???console.log(this.responseText);}

Ajax

原文地址:https://www.cnblogs.com/xiukang/p/9639068.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved