ajax & jsonp & img
ajax 是一种请求服务器的方式,核心是XMLHttpRequest对象;
优点是无需刷新页面,
缺点是不能跨域请求。
/*
* Ajax direacted by Zakas
*
* Ajax.get("url?p=value", function (data) { // handle data }, false);
*
* Ajax.post("url",{
* data : "p=value&id=001",
* callback : function (data) { // handle data },
* async : false
* });
*
*/
var Ajax = (function () { "use strict"; var ajax = { // 惰性载入函数
createXHR: (function () { if (window.XMLHttpRequest) { // 不论new多少次XHR,if只需判断一次
return function () {
return new XMLHttpRequest();
};
} else { // only for ie 6 hack
return function () {
return new ActiveXObject("Microsoft.XMLHTTP");
};
}
}()), get: function (url, callback, async) { var XHR = this.createXHR(); // 默认异步请求
if (async !== false) {
async = true;
} if (async) { // 异步请求
XHR.onreadystatechange = function () { if (XHR.readyState === 4 && XHR.status === 200) {
callback(XHR.responseText); // 销毁不用的对象,因为每次ajax请求都会创建一个新的XHR
XHR = null;
}
} XHR.open("get", url, true);
XHR.send(null);
} else { // 同步请求,返回结果前停止解析上下文
XHR.open("get", url, false);
XHR.send(null);
callback(XHR.responseText);
XHR = null;
}
}, post: function (url, option) { var XHR = this.createXHR();
data = option.data,
callback = option.callback,
async = option.async; if (async !== false) {
async = true;
} if (async) { XHR.onreadystatechange = function () { if (XHR.readyState === 4 && XHR.status === 200) {
callback(XHR.responseText);
XHR = null;
}
} XHR.open("post", url, true);
XHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
XHR.send(data);
} else {
XHR.open("post", url, false);
XHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
XHR.send(data);
callback(XHR.responseText);
XHR = null;
}
}
}; return ajax;
}());
页面中常见的能够跨域进行http请求的有两个标签:<script src=""></script> && <img src="" />
jsonp(JSON with padding)的原理就是<script src=""></script>
// jsonp 是一种可以通过约定进行自定义回调函数的跨域脚本 // 预先定义回调函数
function handleResponse (data) {
// data is a json from remote-source-domain, now deal it in this.
} var script = document.createElement("script");
script.src = "http://remote-source-domain?callback=handleResponse"; // 远程脚本中的数据:handleResponse({data: "json", time: "2014-06-11"});
document.getElementsByTagName("head")[0].appendChild(script); // 当脚本被加载到文档中时,handleResponse函数立即执行
IMG
// 当img对象被赋予src属性时立即发生http请求
var img = new Image();
img.src = "http://remote-source-domain"; // 此时发生了http请求,远程资源被加载到本地 // 图片的预加载
var imgArr = ["0.jpg", "1.jpg", "2.jpg"],
len = imgArr.length,
i; for (i = 0; i < len; i++) {
img.src = imgArr[i];
}
ajax & jsonp & img的更多相关文章
- PHP AJAX JSONP实现跨域请求使用实例
在之前我写过“php返回json数据简单实例”,“php返回json数据中文显示的问题”和“在PHP语言中使用JSON和将json还原成数组”.有兴趣的童鞋可以看看 今天我写的是PHP AJAX JS ...
- AJAX JSONP源码实现(原理解析)
关于JSONP以及跨域问题,请自行搜索. 本文重点给出AJAX JSONP的模拟实现代码,代码中JSONP的基本原理也一目了然. <html xmlns="http://www.w3. ...
- 项目中关于ajax jsonp的使用
项目中关于ajax jsonp的使用,出现了问题:可以成功获得请求结果,但没有执行success方法总算搞定了,记录一下 function TestAjax() { $.ajax({ ...
- 跨域请求之jQuery的ajax jsonp的使用解惑
前天在项目中写的一个ajax jsonp的使用,出现了问题:可以成功获得请求结果,但没有执行success方法,直接执行了error方法提示错误——ajax jsonp之前并没有用过,对其的理解为跟普 ...
- jQuery的ajax jsonp跨域请求
了解:ajax.json.jsonp.“跨域”的关系 要弄清楚以上ajax.json.jsonp概念的关系,我觉得弄清楚ajax是“干什么的”,“怎么实现的”,“有什么问题”,“如果解决存在的问题”等 ...
- 跨域请求jQuery的ajax jsonp使用常见问题解答
前天在项目中写了ajax jsonp的使用,出现了问题:能够成功获得请求结果,但没有运行success方法,直接运行了error方法提示错误--ajax jsonp之前并没实用过.对其的理解为跟普通的 ...
- C# WebClient、jQuery ajax jsonp实现跨域
WebClient 无传输数据获取 Uri uri = new Uri(allURL); WebClient wc = new WebClient(); wc.Encoding = System.Te ...
- 跨域Ajax -- jsonp和cors
跨域Ajax - jsonp - cors 参考博客: http://www.cnblogs.com/wupeiqi/articles/5703697.html http://www.cnblogs. ...
- JQuery Ajax jsonp
JQuery ajax jsonp $.ajax({ method:"POST", url:"http://localhost:8081/ChenLei/PeopleSe ...
- ASP.NET 跨域请求之jQuery的ajax jsonp的使用解惑 (转载)
前天在项目中写的一个ajax jsonp的使用,出现了问题:可以成功获得请求结果,但没有执行success方法,直接执行了error方法提示错误——ajax jsonp之前并没有用过,对其的理解为跟普 ...
随机推荐
- 在git 服务器挂载新的项目
1.cmd 到新创建的文件夹位置,然后 执行该命令git init --bare 2.在客户端克隆项目到文件夹(空),将项目复制到该空文件夹下,然后打开git extent,提交并推送
- Android各个文件夹对应的分辨率?
- MFC读取XML文件并解析
现在经常会对XML文件进行操作,怎么在MFC下去读和解析XML文件呢?直接上代码: 首先得等在stdafx.h中加入这句,以引入MSXML命名空间 #import <msxml3.dll> ...
- PAT1069. The Black Hole of Numbers
//这是到水题,之前因为四位数的原因一直不能A,看了别人的程序,才明白,不够四位的时候没考虑到,坑啊.....脸打肿 #include<cstdio>#include<algorit ...
- Spring 注解实体类中非数据库字段属性
解决办法:在属性的get方法上加上一段注解标识它是临时属性,不是数据库字段就OK @Transient public List<Reverts> getChildList() { retu ...
- Where is "Active Directory Information Extractor"?
My friend she showed me a screenshot as below yesterday. The name of this document is “EnCase Forens ...
- s3c6410_u-boot-2010.03移植
开发环境: 开发板 FriendlyARM Tiny6410 主机 CentOS release 6.4 (Final) 参考: http://www.cnblogs.com/plinx/archiv ...
- Silverlight Color的颜色值
1.MainPage.xaml <UserControl xmlns:SysManage="clr-namespace:Application" x:Class=" ...
- Yii 增删改查 测试记录
亲们, 我是yii小白 不要笑话我奥.今天白天写一个管理模块涉及到 yii ar 下的 curd 操作,做 update 操作时纠结了好久,今天晚上花点时间学习, 下面写下我的测试记录 代码如下: ...
- 【转】Spark性能测试报告
RDD可以很好地适用于支持数据并行的批量分析应用,包括数据挖掘,机器学习,图算法等,因为这些程序通常都会在很多记录上执行相同的操作.RDD不太适合那些异步更新共享状态的应用,例如并行web爬行器.因此 ...