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的更多相关文章

  1. PHP AJAX JSONP实现跨域请求使用实例

    在之前我写过“php返回json数据简单实例”,“php返回json数据中文显示的问题”和“在PHP语言中使用JSON和将json还原成数组”.有兴趣的童鞋可以看看 今天我写的是PHP AJAX JS ...

  2. AJAX JSONP源码实现(原理解析)

    关于JSONP以及跨域问题,请自行搜索. 本文重点给出AJAX JSONP的模拟实现代码,代码中JSONP的基本原理也一目了然. <html xmlns="http://www.w3. ...

  3. 项目中关于ajax jsonp的使用

    项目中关于ajax jsonp的使用,出现了问题:可以成功获得请求结果,但没有执行success方法总算搞定了,记录一下 function TestAjax() {    $.ajax({       ...

  4. 跨域请求之jQuery的ajax jsonp的使用解惑

    前天在项目中写的一个ajax jsonp的使用,出现了问题:可以成功获得请求结果,但没有执行success方法,直接执行了error方法提示错误——ajax jsonp之前并没有用过,对其的理解为跟普 ...

  5. jQuery的ajax jsonp跨域请求

    了解:ajax.json.jsonp.“跨域”的关系 要弄清楚以上ajax.json.jsonp概念的关系,我觉得弄清楚ajax是“干什么的”,“怎么实现的”,“有什么问题”,“如果解决存在的问题”等 ...

  6. 跨域请求jQuery的ajax jsonp使用常见问题解答

    前天在项目中写了ajax jsonp的使用,出现了问题:能够成功获得请求结果,但没有运行success方法,直接运行了error方法提示错误--ajax jsonp之前并没实用过.对其的理解为跟普通的 ...

  7. C# WebClient、jQuery ajax jsonp实现跨域

    WebClient 无传输数据获取 Uri uri = new Uri(allURL); WebClient wc = new WebClient(); wc.Encoding = System.Te ...

  8. 跨域Ajax -- jsonp和cors

    跨域Ajax - jsonp - cors 参考博客: http://www.cnblogs.com/wupeiqi/articles/5703697.html http://www.cnblogs. ...

  9. JQuery Ajax jsonp

    JQuery ajax jsonp $.ajax({ method:"POST", url:"http://localhost:8081/ChenLei/PeopleSe ...

  10. ASP.NET 跨域请求之jQuery的ajax jsonp的使用解惑 (转载)

    前天在项目中写的一个ajax jsonp的使用,出现了问题:可以成功获得请求结果,但没有执行success方法,直接执行了error方法提示错误——ajax jsonp之前并没有用过,对其的理解为跟普 ...

随机推荐

  1. 简单几步优化技巧令你的Windows7系统加速

    就算有再高的硬件配置,系统用久了还是会变慢,xp如此,win7同样是如此.其实系统用的如何完全在于个人使用习惯,只要掌握了以下三种方法,就可以让你的win7运行速度大大提升. 1.修改启动项程序 在你 ...

  2. C语言,不是从hello world开始

    开始看C语言,主要是复习,所以就没必要从hello world开始了,写点例子熟悉下就好了. 使用公式℃=(5/9)(℉-32)打印下列华氏温度与摄氏温度对照表: #include <stdio ...

  3. 在 MVC 控制器中使用 构造函数时行依赖注入 (IoC)

    在 Controller 中使用 构造函数进行依赖注入 (IoC) 1. Controller 代码: ICard card; ICardCategory cardCategory; public C ...

  4. android网络判断

    //ConnectivityManager管理网络连接相关的操作 ConnectivityManager connectivityManager = (ConnectivityManager) con ...

  5. android 上传文件用php程序在服务端接受(一)

    php服务端接受程序..file_up.php. <?php /* require_once('lib/session_config.php'); require_once('lib/flydc ...

  6. SQL:认识数据库约束

    讲了关于数据库的很多内容,也讲了很多约束,对唯一和主键.核查和规则.外键约束很感兴趣. 一.唯一和主键(实体完整性) 区别: 数量:一张表只可以有一个主键约束,却可以有多个唯一约束. 是否空值:主键不 ...

  7. 简化对象extend拓展

    发现对对象继承或拷贝的时候,总是要$点来点去好麻烦,我的解决办法如下: (function(){ Object.prototype.extend = function(o){ $.extend(tru ...

  8. CLRS:Max_sunsequence_sum O(n*n) O(nlgn) O(n)

    #include<stdio.h>#include<stdlib.h>#include<time.h>#define ARRAY_SIZE 1000int buf ...

  9. 安装package.js

  10. Android事件

    1.Java package com.fish.helloworld; import android.app.Activity; import android.graphics.Color; impo ...