
如何实现ajax请求?
1、创建XMLHttpRequest实例;
var xhr;if(window.XMLHttpRequest) {
//ie7+,firefox,chrome,safari,opera
xhr = new XMLHttpRequest();}else {
//ie5,ie6
xhr = new ActiveXObject("Microsoft.XMLHTTP");}
2、监听readystatechange事件,并通过readyState属性来判断请求状态;
xhr.onreadystatechange = function() {
if(xhr.readyState==4 && xhr.status==200) {
console.log(xhr.responseText);
}}
3、调用open()方法指定请求类型和地址;
一个基于官方 Mailtrap Node.js SDK 构建的 ASE 技能,用于发送事务邮件并与 Mailtrap 沙盒或生产流程协作。它非常适合需要 API 驱动邮件投递、测试邮箱验证和结构化消息自动化的代理工作流。
xhr.open("GET", "xhr_info.txt");
4、调用send()方法发送请求即可。
xhr.send(null);
完整代码
var xhr;
if(window.XMLHttpRequest) {
//ie7+,firefox,chrome,safari,opera
xhr = new XMLHttpRequest();
} else {
//ie5,ie6
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.onreadystatechange = function() {
if(xhr.readyState==4 && xhr.status==200) {
console.log(xhr.responseText);
}
}
xhr.open("GET", "xhr_info.txt", true);
xhr.send(null);
推荐教程:《JS教程》










