使用JavaScript的XMLHttpRequest发送请求
来源:互联网 发布:免备案域名购买 编辑:程序博客网 时间:2024/04/29 17:27
使用XMLHttpRequest对象分为4部完成:
1.创建XMLHttpRequest组建
2.设置回调函数
3.初始化XMLHttpRequest组建
4.发送请求
实例代码:
var userName; var passWord; var xmlHttpRequest; //XmlHttpRequest对象 function createXmlHttpRequest(){ if(window.ActiveXObject){ //如果是IE浏览器 return new ActiveXObject("Microsoft.XMLHTTP"); }else if(window.XMLHttpRequest){ //非IE浏览器 return new XMLHttpRequest(); } } function onLogin(){ userName = document.f1.username.value; passWord = document.f1.password.value; var url = "LoginServlet?username="+userName+"&password="+passWord+""; //1.创建XMLHttpRequest组建 xmlHttpRequest = createXmlHttpRequest(); //2.设置回调函数 xmlHttpRequest.onreadystatechange = zswFun; //3.初始化XMLHttpRequest组建 xmlHttpRequest.open("POST",url,true); //4.发送请求 xmlHttpRequest.send(null); } //回调函数 function zswFun(){ if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){ var b = xmlHttpRequest.responseText; if(b == "true"){ alert("登录成功!"); }else{ alert("登录失败!"); } } }
11
var xmlhttp;
function verify1() {
var username = document.getElementById("username").value;
//确定浏览器
if(window.XMLHttpRequest) {
//针对FireFox、Mozillar、Opera、Safari、IE7、IE8
//创建XMLHttpRequest对象
xmlhttp = new XMLHttpRequest();
//修正某些浏览器的BUG
if(xmlhttp.overrideMimeType) {
xmlhttp.overrideMimeType("text/html");
}
}else if(window.ActiveXObject){
//针对IE5、IE5.5、IE6
//这两个为插件名称作为参数传递,为了创建ActiveXObject
var activeName = ["MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
for(var i=0;i>activeName.length();i++) {
try{
//非别取出,如果创建成功则终止循环,如果失败则会抛出异常继续循环
xmlhttp = new ActiveXObject(activeName[i]);
break;
}catch(e){
}
}
}
//确定XMLHttpRequest是否创建成功
/*if(!xmlhttp) {
alert("XMLHttpRequest创建失败!");
return;
}else {
alert("XMLHttpRequest创建成功!"+xmlhttp);
}*/
//注册回调函数
xmlhttp.onreadystatechange=callback;
url = "classisservlet?name="+username;
//设置连接信息
//1.是http请求的方式
//2.是服务器的地址
//3.是采用同步还是异步,true为异步
//xmlhttp.open("GET",url,true);
//post请求与get请求的区别
//第一个参数设置成post第二个只写url地址,第三个不变
xmlhttp.open("POST","classisservlet",true);
//post请求要自己设置请求头
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//发送数据,开始与服务器进行交互
//post发送请求
xmlhttp.send("name="+username);
}
function callback() {
//接收响应数据
//判断对象状态是否交互完成,如果为4则交互完成
if(xmlhttp.readyState == 4) {
//判断对象状态是否交互成功,如果成功则为200
if(xmlhttp.status == 200) {
//接收数据,得到服务器输出的纯文本数据
var response = xmlhttp.responseText;
//得到div的节点将数据显示在div上
var divresult = document.getElementById("result");
divresult.innerHTML = response;
}
}
function verify1() {
var username = document.getElementById("username").value;
//确定浏览器
if(window.XMLHttpRequest) {
//针对FireFox、Mozillar、Opera、Safari、IE7、IE8
//创建XMLHttpRequest对象
xmlhttp = new XMLHttpRequest();
//修正某些浏览器的BUG
if(xmlhttp.overrideMimeType) {
xmlhttp.overrideMimeType("text/html");
}
}else if(window.ActiveXObject){
//针对IE5、IE5.5、IE6
//这两个为插件名称作为参数传递,为了创建ActiveXObject
var activeName = ["MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
for(var i=0;i>activeName.length();i++) {
try{
//非别取出,如果创建成功则终止循环,如果失败则会抛出异常继续循环
xmlhttp = new ActiveXObject(activeName[i]);
break;
}catch(e){
}
}
}
//确定XMLHttpRequest是否创建成功
/*if(!xmlhttp) {
alert("XMLHttpRequest创建失败!");
return;
}else {
alert("XMLHttpRequest创建成功!"+xmlhttp);
}*/
//注册回调函数
xmlhttp.onreadystatechange=callback;
url = "classisservlet?name="+username;
//设置连接信息
//1.是http请求的方式
//2.是服务器的地址
//3.是采用同步还是异步,true为异步
//xmlhttp.open("GET",url,true);
//post请求与get请求的区别
//第一个参数设置成post第二个只写url地址,第三个不变
xmlhttp.open("POST","classisservlet",true);
//post请求要自己设置请求头
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//发送数据,开始与服务器进行交互
//post发送请求
xmlhttp.send("name="+username);
}
function callback() {
//接收响应数据
//判断对象状态是否交互完成,如果为4则交互完成
if(xmlhttp.readyState == 4) {
//判断对象状态是否交互成功,如果成功则为200
if(xmlhttp.status == 200) {
//接收数据,得到服务器输出的纯文本数据
var response = xmlhttp.responseText;
//得到div的节点将数据显示在div上
var divresult = document.getElementById("result");
divresult.innerHTML = response;
}
}
}
0 0
- 使用JavaScript的XMLHttpRequest发送请求
- 使用JavaScript的XMLHttpRequest发送请求
- 使用JavaScript的XMLHttpRequest发送请求
- 使用JavaScript的XMLHttpRequest发送请求
- 使用JavaScript的XMLHttpRequest发送POST、GET请求以及接收返回值
- 使用JavaScript的XMLHttpRequest发送POST、GET请求以及接收返回值
- 使用JavaScript的XMLHttpRequest发送POST、GET请求以及接收返回值
- 基本Javascript创建XMLHTTPRequest发送Ajax请求
- 使用XMLHttpRequest对象向服务器发送简单的请求
- XMLHttpRequest send 发送请求
- XMLHttpRequest发送POST请求
- XMLHttpRequest发送XML请求
- XMLHttpRequest发送JSON请求
- Ajax - 使用XMLHttpRequest对象向服务器发送简单请求
- 使用XMLHttpRequest对象向服务器发送简单请求
- 如何使用Ajax(XMLHttpRequest)发送带参数的请求,以及如何在Servlet中获取请求中的参数
- AJAX 多次发送异步请求的 XmlHttpRequest 对象的设置
- 创建XMLHttpRequest对象并向服务器发送请求的方法
- C++中修改类的私有属性的方法
- HTTP协议之消息报头
- C语言不用系统函数对字符串进行截取(代码贴过来无缩进,尽请谅解)
- [leetcode] 200.Number of Islands
- java面向对象(抽象类)
- 使用JavaScript的XMLHttpRequest发送请求
- 《大话数据结构》第3章 线性表 3.8.2 单链表的删除
- python学习笔记(1)
- typedef的使用
- ListView回收机制
- linux /proc/net/arp 文件
- 如何查看进程的完整路径?
- 2010. Sum of Consecutive Prime Numbers
- Linux中更改文件属性常用命令