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之前并没有用过,对其的理解为跟普 ...
随机推荐
- Hive On Spark和SparkSQL
SparkSQL和Hive On Spark都是在Spark上实现SQL的解决方案.Spark早先有Shark项目用来实现SQL层,不过后来推翻重做了,就变成了SparkSQL.这是Spark官方Da ...
- cacti yum快速部署
简述:本来是编译安装的,由于编译过程中库文件关联太多,安装文件一定要有顺序性,报错太多,到了rrdtool这一步说什么都安装不过去了,由于时间问 题,选择了yum安装,对于编译安装,这个要简单多了,她 ...
- JavaScript 性能优化1
一直在学习javascript,也有看过<犀利开发Jquery内核详解与实践>,对这本书的评价只有两个字犀利,可能是对javascript理解的还不够透彻异或是自己太笨,更多的是自己不擅于 ...
- 要在一般处理程序中获取其他页面的session值
1.要在一般处理程序中获取其他页面的session值,需要引用名空间: using System.Web.SessionState; 2.然后继承一个接口:IRequiresSessionState, ...
- 对像转成 和 byte 互转类库方法
using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serializatio ...
- Oracle 查询字段在什么表
-- 查询字段在什么表 select * from all_tab_cols t where t.column_name='ABC'; -- 查询字段在什么表并且 判断是否是主键 select * f ...
- hdu2093
#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> ...
- aspx网页相对布局
网页的布局 <body bgcolor="#b6b7bc"> <form id="form1" runat="server" ...
- Tomcat启动过程(一):从BootStrap到EndPonit
Tomcat版本tomcat6.0.44观察catalina.sh文件,启动入口:org.apache.catalina.startup.Bootstrap,该类main方法执行下面三个方法----- ...
- Spring Controller 获取请求参数的几种方法
1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交.若"Content-Type"="application/ ...