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之前并没有用过,对其的理解为跟普 ...
随机推荐
- 【测试】手工搭建DG
前言:(一)准备工作: 1.数据库要处于归档模式: 2.监听参数:local_listener 默认值为空--1521 3.关闭闪回(可能会触发数据库的bug,备库不能开闪回) 4.如果有外部表,外部 ...
- 学习练习 java产生6个不同的数字
public static void main(String[] args) { Random r=new Random(); int arr[]=new int[6]; for(int i=0;i& ...
- PAT1013. Battle Over Cities(邻接矩阵、邻接表分别dfs)
//采用不同的图存储结构结构邻接矩阵.邻接表分别dfs,我想我是寂寞了吧,应该试试并查集,看见可以用并查集的就用dfs,bfs代替......怕了并查集了 //邻接矩阵dfs #include< ...
- jQuery遍历 slice()方法
今天做页面,遇到一个滚动的swipe,需要4个<li> 一组,然后在外层加个<ul>,方法如下: $('.xxxxx li').each(function(n){ $('.xx ...
- tech
流式计算框架storm.spark.genfire.esper(CEP)
- JS产生四位随机数的方法
<script>var charactors="1234567890"; var value='',i; for(j=1;j<=4;j++){ i = parse ...
- 10 Code Coverage Tools for C & C++
Code coverage is a measure used in software testing that describes the degree to which the source co ...
- Boost C++: 网络编程1
#include <iostream> #include <boost/asio.hpp> #include <boost/config/compiler/visualc ...
- AnyCAD C++ SDK与OpenCASCADE互操作
AnyCAD SDK有.Net和C++两个版本,使用C++版本的AnyPlatformOcc模块可以实现与OpenCASCADE互操作. C++版本(VS2010 32bit)下载 在AOBridge ...
- iPhone和iPad版本的分辨率a