XmlHttpRequest 使用
1. IE7以后对xmlHttpRequest 对象的创建在不同浏览器上是兼容的。下面的方法是考虑兼容性的,实际项目中一般使用Jquery的ajax请求,可以不考虑兼容性问题。
function getHttpObject() {
var xhr=false;
if (windows.XMLHttpRequest)
xhr=new XMLHttpRequest();
else if (window.ActiveXObject)
{
xhr=new ActiveXObject("Microsoft.XMLHttp");
}
return xhr;
}
2. XMLHttpRequest的属性及方法
| 属性 | 描述 |
| onreadystatechange | 每个状态改变都会触发,通常会调用一个javascript函数 |
| readyState | 请求的状态,5个值; 0:为初始化,1:正在加载;2:已经加载,3:交互中,4:完成 |
| responseText | 服务器的响应,表示为字符串 |
| responseXML | 服务器的响应,表示为XML,可以解析为DOM对象 |
| status | 服务器的HTTP状态码(200:ok,304:not modified,404:Not Found 等) |
| statusText | Http状态码的相应文本(ok或Not Found) |
| 方法 | 描述 |
| abort() | 停止当前请求 |
| getAllResponseHeaders() | 把HTTP请求的所有响应首部作为键/值对返回 |
| getResponseHeader(“header”) | 返回指定键的首部串值 |
| open(“method”,”url”) | 建立对服务器的调用,Method可以是GET,POST或PUT,URL可以是相对或绝对URL |
| send(content) | 向服务器发送请求 |
| setRequestHeader(“header”,”value”) | 把指定首部设置为所提供的值。在设置任何首部之前必须调用open() |
手写一个Ajax请求的例子:
$(function(){
$("#id").onclick(tunction(){
var request=new XMLHttpRequest();
var url="http://www.baidu.com";
var method="GET";
request.open(method,url);
request.send(null);
request.onreadystatechange=function(){
if (request.readyState==4&&(request.status==200 || request.status==304))
alert (request.reponseText);
//如果返回的是html 标签,则可以使用
//$(“#id2”).innerHtml=request.reponseText;
//如果返回的xml格式,则需要将结果通过getElementByTagName(“”)[index]解析
//alert(request.reponseXML.getElementByTagName(“”)[index])
}
})
})
这里再插入一下window.onload 和$(function(){})($(document).ready(function(){}))的区别:
1. window.onload 必须等到页面内包括图片的所有元素加载完毕才能执行
$(function(){}) 是DOM结构绘制完毕后就执行,不必等到加载完毕。
2. 编写个数不同
window.onload 不能同时编写多个,如果有多个window.onload方法,只会执行一个
$(function(){}) 可以同时编写多个,并且都会得到执行
3. 简化写法
window.onload 没有简写方法,但可以使用$(window).load(function(){})代替
$(function(){})实际是$(document).ready(function(){})的缩写方法
$(window).load(function(){})一般情况下都会在$(function(){})之后执行,但是如果使用了Iframe的话
可能不一样,附上jquery 1.8.2的源码,IE只有在不是frame的情况下才和其他浏览器一样,先执行$(function(){})
对于嵌入frame中的页面,是绑定到 load 事件中,因此会先执行load 事件。
jQuery.ready.promise = function( obj ) {
if ( !readyList ) {
readyList = jQuery.Deferred();
// Catch cases where $(document).ready() is called after the browser event has already occurred.
// we once tried to use readyState "interactive" here, but it caused issues like the one
// discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15
if ( document.readyState === "complete" ) {
// Handle it asynchronously to allow scripts the opportunity to delay ready
setTimeout( jQuery.ready, 1 );
// Standards-based browsers support DOMContentLoaded
} else if ( document.addEventListener ) {
// Use the handy event callback
document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
// A fallback to window.onload, that will always work
window.addEventListener( "load", jQuery.ready, false );
// If IE event model is used
} else {
// Ensure firing before onload, maybe late but safe also for iframes
document.attachEvent( "onreadystatechange", DOMContentLoaded );
// A fallback to window.onload, that will always work
window.attachEvent( "onload", jQuery.ready );
// If IE and not a frame
// continually check to see if the document is ready
var top = false;
try {
top = window.frameElement == null && document.documentElement;
} catch(e) {}
if ( top && top.doScroll ) {
(function doScrollCheck() {
if ( !jQuery.isReady ) {
try {
// Use the trick by Diego Perini
// http://javascript.nwbox.com/IEContentLoaded/
top.doScroll("left");
} catch(e) {
return setTimeout( doScrollCheck, 50 );
}
// and execute any waiting functions
jQuery.ready();
}
})();
}
}
}
return readyList.promise( obj );
};
XmlHttpRequest 使用的更多相关文章
- JavaScript权威设计--跨域,XMLHttpRequest(简要学习笔记十九)
1.跨域指的是什么? URL 说明 是否允许通信 http://www.a.com/a.jshttp://www.a.com/b.js 同一域名下 允许 http://www.a.com/lab/a. ...
- AJAX的核心XMLHttpRequest对象
为了实现异步通讯,提高用户体验度,而将很多旧知识(XML,DOM,JavaScript,HTML,jQuery,Css...)重新融合程一个新的知识框架.而XMLHttpRequest对象则是其中的重 ...
- XMLHTTPRequest对象的创建与浏览器的兼容问题
MLHttpRequest 对象是AJAX功能的核心,要开发AJAX程序必须从了解XMLHttpRequest 对象开始. 了解XMLHttpRequest 对象就先从创建XMLHttpRequest ...
- 部分安卓手机微信浏览器中使用XMLHttpRequest 2上传图片显示字节数为0的解决办法
前端JS中使用XMLHttpRequest 2上传图片到服务器,PC端和大部分手机上都正常,但在少部分安卓手机上上传失败,服务器上查看图片,显示字节数为0.下面是上传图片的核心代码: HTML < ...
- Ajax 与 XmlHttpRequest
AJAX描述了确保Web应用在Web服务器请求新数据的情况下也能(几乎)实时反应的一种方法.具体地说,AJAX只是一些建立已久的技术的相互作用,从HTML.XHTML和HTTP,到JavaScript ...
- js中XMLHttpRequest对象实现GET、POST异步传输
js中XMLHttpRequest对象实现GET.POST异步传输 /* * 统一XHR接口 */ function createXHR() { // IE7+,Firefox, Opera, Chr ...
- AJAX初探,XMLHttpRequest介绍
AJAX初探,XMLHttpRequest介绍 AJAX AJAX = Asynchronous JavaScript and XML. 异步的JavaScript和XML. AJ ...
- 大熊君学习html5系列之------XHR2(XMLHttpRequest Level 2)
一,开篇分析 Hi,大家好!大熊君又和大家见面了,(*^__^*) 嘻嘻……,这系列文章主要是学习Html5相关的知识点,以学习API知识点为入口,由浅入深的引入实例, 让大家一步一步的体会" ...
- 手动封装js原生XMLHttprequest异步请求
Code Object.extend =function(targetObj,fnJson){ //扩展方法,类似于jQuery的$.extend,可以扩展类的方法,也可以合并对象 for(var f ...
- XMLHttpRequest对象用法
xmlhttprequest is what? 用户后台与服务器交换数据. 可以在不重新加载页面的情况下更新网页: 在页面已加载后从服务器请求数据: 在页面已加载后从服务器接收数据: 在后台向服务器发 ...
随机推荐
- 使用Xcode Instruments Leak解决内存泄漏问题
iOS 5.0之后apple引入了Xcode编译器特性ARC(Automatic Reference Counting,自动引用计数)来帮助开发者管理内存,但为了追求app的高性能与减少安装包大小,工 ...
- (转载)设计模式学习笔记(十一)——Facade外观模式
(转载)http://www.cnblogs.com/kid-li/archive/2006/07/10/446904.html Facade外观模式,是一种结构型模式,它主要解决的问题是:组件的客户 ...
- 如何删除MyEclipse(eclipse)中不需要的workspace
在安装目录下,打开eclipse/configuration/.settings,用记事本打开org.eclipse.ui.ide.prefs文件 #Wed Mar 11 14:41:21 CST 2 ...
- C# Protect the Password inside a TextBox ZZ
If the Text property is called, it will send an WM_GETTEXT message, so it will surely be an internal ...
- loadrunner调用plink,远程linux执行shell命令
loadrunner调用plink,远程linux执行shell命令 脚本: Action() { char* cmd; cmd = lr_eval_string("C:\\\&qu ...
- leecode 回文字符串加强版
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- ubuntukylin(64bit)安装推荐
UbuntuKylin是Ubuntu社区中面向中文用户的Ubuntu衍生版本,与麒麟系统没有关系.它是由工信部软件.集成电路促进中心(CSIP).国防科技大学(NUDT)与国际著名开源社区UBUNTU ...
- 零基础学习视频解码之FFMpeg中比较重要的函数以及数据结构
http://www.cnblogs.com/tanlon/p/3879081.html 在正式开始解码练习前先了解下关于FFmpeg中比较重要的函数以及数据结构. 1. 数据结构: (1) AVF ...
- 30种mysql优化sql语句查询的方法
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索 ...
- ReactiveCocoa框架学习1
写block直接使用inline block的声明类型 在ARC中使用strong,如果不使用strong,则会被销毁 在非ARC中使用copy block在开发中的使用场景 把block保存到对象中 ...