ajax的基础知识
来源:互联网 发布:看韩国电影的软件 编辑:程序博客网 时间:2024/06/05 19:08
一 ajax是不刷新页面从服务器端获取数据的方法
1 ajax的核心对象XMLHttpRequest对象
用法:var xhr = newXmlHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readystate ==4){
if(xhr.state==200&&xhr.state<300||xhr.state==304){
alert(xhr.responseText)
}else{
alert(error);
}
}
}
xhr.open("get","index.html",true) ; //xhr.open("请求类型如:get",“url”,"是否异步发送请求的布尔值");
xhr.setRequestHeader("myheader","myvalue");
xhr.send(null); send接收的参数为是否作为请求主体发送,如果不作为则传入null,这个参数是必须的
2跨域解决方案CORS(跨域资源共享)
IE8通过XDomainRequest对象支持CORS,其他浏览器通过XHR原生对象支持CORS
另外两种跨域通信技术:Jsonp和图像Ping
原生xhr支持:检查是否存在withCredentails属性,再检测XDomainRequest对象是否存在
function createCORSRequest(method,url){
var xhr = new XmlHttpRequest();
if("withCredentails" in xhr){
xhr.open(method,url,true);
}else if(typeof XDomainRequest != "undefined"){
var xhr = new XDomainRequest();
xhr.open(method,url);
}else{
xhr = null;
}
return xhr;
}
var request = createCORSRequest("get","http://www.baidu.com");
if(request){
request.onload= function(){
//对responseText进行处理
};
request.send();
}
- ajax的基础知识
- ajax的基础知识
- Ajax的基础知识
- ajax的基础知识
- ajax基础知识,原生态ajax示例,jquery的ajax示例
- AJAX 基础知识
- Ajax基础知识
- Ajax 基础知识
- ajax基础知识
- AJAX基础知识
- AJAX基础知识
- ajax -基础知识
- AJAX基础知识
- 基础知识--Ajax
- AJAX基础知识
- Ajax基础知识
- Ajax基础知识
- ajax基础知识
- 下载ubuntu16.04LTS过程记录
- 18.描述一下你最常用的编程风格
- VMware Workstation 12.5.7 PRO 安装 CentOS7
- CAN2.0的标准帧和扩展帧格式
- 数据库中空字符串和NULL值两个概念的区别
- ajax的基础知识
- C# Ftp基础操作
- 算法作业系列14——Coin Change
- 19.描述下常用的重构技巧
- LINQ与Lambda表达式的查询语句写法
- 整型
- TimeUnit 使用
- 20.知识面、CDN实现原理
- 运维第一次作业