function createXHR () {
if (typeof XMLHttpRequest != "undefined") {
return new XMLHttpRequest();
} else if (typeof ActiveXObject != "undefined") { // < Ie7
if (typeof arguments.callee.activeXString != "string") {
var version = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"],
i, len;
for ( i = 0, len = version.length; i < len; i++) {
try {
new ActiveXObject(version[i]);
arguments.callee.activeXString = version[i];
break;
} catch (ex) {}
}
}
return new ActiveXObject(arguments.callee.activeXString);
} else {
throw new Error("No Support For XHR");
}
}
var xhr = createXHR();
alert(xhr); // [object XMLHttpRequest]
var xhr = createXHR();
// GET方式同步打开example.txt文件
// 同步:javascript代码会等待服务器响应后执行
xhr.open("get", "example.txt", false);
xhr.send(null);
// status代表响应的http状态
// 200代表ok,304表示缓存
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){
alert(xhr.responseText); // 返回响应的文本,123456
} else {
alert("Request was unsuccessful: " + xhr.status);
}
var xhr = createXHR();
// xhr.readyState表示请求/响应的当前状态,4代表已经接受了全部的响应数据
// 另外只要xhr.readyState的值发生了改变,那么xhr.onreadystatechange事件就会触发
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){
alert(xhr.responseText);
} else {
alert("Request was unsuccessful: " + xhr.status);
}
}
};
xhr.open("get", "example.txt", true);
xhr.send(null);
var xhr = createXHR();
// xhr.readyState表示请求/响应的当前状态,4代表已经接受了全部的响应数据
// 另外只要xhr.readyState的值发生了改变,那么xhr.onreadystatechange事件就会触发
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){
alert(xhr.responseText);
} else {
alert("Request was unsuccessful: " + xhr.status);
}
}
};
xhr.open("get", "example.txt", true);
// 必须在open()之后调用
xhr.setRequestHeader("name", "zhang"); // 在example.txt的http中可以看到接受的 "name" : "zhang"
xhr.send(null);
var xhr = createXHR();
// xhr.readyState表示请求/响应的当前状态,4代表已经接受了全部的响应数据
// 另外只要xhr.readyState的值发生了改变,那么xhr.onreadystatechange事件就会触发
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){
// 获取响应头的Content-Type
var connection = xhr.getResponseHeader("Content-Type");
// alert(connection); // text/plain
// 获取所有的响应信息
var all = xhr.getAllResponseHeaders();
alert(all);
} else {
alert("Request was unsuccessful: " + xhr.status);
}
}
};
xhr.open("get", "example.txt", true);
xhr.send(null);
/**
url : 不带请求的url
name : 请求键
value : 请求值
return : 带请求字符串的url
*/
function addURLParam(url, name, value) {
url += (url.indexOf("?") == -1 ? "?" : "&");
url += encodeURIComponent(name) + "=" + encodeURIComponent(value);
return url;
}
var xhr = createXHR();
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){
alert(xhr.responseText);
} else {
alert("Request was unsuccessful: " + xhr.status);
}
}
};
var url = "example.txt";
url = addURLParam(url, "name", "zhang");
url = addURLParam(url, "age", "23");
// 请求的url变成了:example.txt?name=zhang&age=23
xhr.open("get", url, true);
xhr.send(null);
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> </style> </head> <body> <form name="myForm" method="post"> 姓名:<input type="text" name="username" /><label id="userLabel">请输入用户名</label><br/> 密码:<input type="password" name="password" /><br/> <input type="submit" value="登录" /><br/> </form> <script src="EventUtil.js"></script> <script src="serialize.js"></script> <script src="ajax.js"></script> <script src="ajaxDo.js"></script> </body> </html>
var EventUtil = {
addEvent : function (element, type, handler) {
if (element.addEventListener)
{
element.addEventListener(type, handler, false);
} else if (element.attachEvent)
{
element.attachEvent("on" + type, handler);
}
}
}
function serialize(form){
var parts = [], field = null, i, len, j, optLen, option, optValue;
for (i=0, len=form.elements.length; i < len; i++){
field = form.elements[i];
switch(field.type){
case "select-one":
case "select-multiple":
if (field.name.length){
for (j=0, optLen = field.options.length; j < optLen; j++){
option = field.options[j];
if (option.selected){
optValue = "";
if (option.hasAttribute){
optValue = (option.hasAttribute("value") ?
option.value : option.text);
} else {
optValue = (option.attributes["value"].specified ?
option.value : option.text);
}
parts.push(encodeURIComponent(field.name) + "=" +
encodeURIComponent(optValue));
}
}
}
break;
case undefined: //字段集
case "file": //文件输入
case "submit": //提交按钮
case "reset": //重置按钮
case "button": //自定义按钮
break;
case "radio": //单选按钮
case "checkbox": //复选框
if (!field.checked){
break;
}
/* 执行默认操作*/
default:
//不包含没有名字的表单字段
if (field.name.length){
parts.push(encodeURIComponent(field.name) + "=" +
encodeURIComponent(field.value));
}
}
}
return parts.join("&");
}
var form = document.forms[0]; // 获取表单
var username = form.elements['username']; // 用户名
var userLabel = document.querySelector("#userLabel"); // 提示标签
EventUtil.addEvent(username, "blur", function() {
var xhr = createXHR();
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){
var text = xhr.responseText;
// 当为true时,提示绿色字体
// 当为false时,提示为红色字体
if(text) {
userLabel.style.color = "green";
userLabel.firstChild.data = "恭喜你,用户名可用";
} else {
userLabel.style.color = "red";
userLabel.firstChild.data = "对不起,该用户已存在";
}
} else {
alert("Request was unsuccessful: " + xhr.status);
}
}
};
// POST请求
xhr.open("post", "dome.php", true);
// 提交的内容类型
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// 将表单序列化
xhr.send(serialize(form));
});
// ...此处省略代码和上面一致
// POST请求
xhr.open("post", "dome.php", true);
// 仅仅这里需要改动,代替之前serialize.js中的函数
xhr.send(new FormData(form));
xhr.open("get", "example.txt", true);
xhr.timeout = 1000; //将超时设置为1 秒钟(仅适用于IE8+)
xhr.ontimeout = function(){
alert("Request did not return in a second.");
};
xhr.send(null);
var xhr = createXHR();
xhr.open("get", "example.txt", true);
xhr.overrideMimeType("text/xml"); // 之前的是text/plain
xhr.send(null);
var xhr = createXHR();
xhr.onload = function(){
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){
alert(xhr.responseText);
} else {
alert("Request was unsuccessful: " + xhr.status);
}
};
xhr.open("get", "example.txt", true);
xhr.send(null);
var xhr = createXHR();
xhr.onprogress = function(event){
var divStatus = document.getElementById("status");
// 计算从响应中已经接收到的数据的百分比
if (event.lengthComputable){
divStatus.innerHTML = "Received " + event.position + " of " +
event.totalSize +" bytes";
}
};
xhr.open("get", "altevents.php", true);
xhr.send(null);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有