Ajax 核心框架函数及例子

所属分类: 网络编程 / AJAX相关 阅读数: 343
收藏 0 赞 0 分享
核心ajax(options)函数中,包含了建立xmlhttprequest,提取数据,判断是否回复成功等,基本满足了日常需求。
复制代码 代码如下:

// A generic function for performming AJAX requests
// It takes one argument, which is an object that contains a set of options
// All of which are outline in the comments, below
function ajax( options ) {
// Load the options object with defaults, if no
// values were provided by the user
options = {
// The type of HTTP Request
type: options.type || "POST",
// The URL the request will be made to
url: options.url || "",
// How long to wait before considering the request to be a timeout
timeout: options.timeout || 5000,
// Functions to call when the request fails, succeeds,
// or completes (either fail or succeed)
onComplete: options.onComplete || function(){},
onError: options.onError || function(){},
onSuccess: options.onSuccess || function(){},
// The data type that'll be returned from the server
// the default is simply to determine what data was returned from the
// and act accordingly.
data: options.data || ""
};
// Create the request object
var xml = new XMLHttpRequest();
// Open the asynchronous POST request
//xml.open("GET", "/some/url.cgi", true);
xml.open("GET",options.url, true);
// We're going to wait for a request for 5 seconds, before giving up
var timeoutLength = 5000;
// Keep track of when the request has been succesfully completed
var requestDone = false;
// Initalize a callback which will fire 5 seconds from now, cancelling
// the request (if it has not already occurred).
setTimeout(function(){
requestDone = true;
}, timeoutLength);
// Watch for when the state of the document gets updated
xml.onreadystatechange = function(){
// Wait until the data is fully loaded,
// and make sure that the request hasn't already timed out
if ( xml.readyState == 4 && !requestDone ) {
// Check to see if the request was successful
if ( httpSuccess( xml ) ) {
// Execute the success callback with the data returned from the server
options.onSuccess( httpData( xml, options.type ) );
// Otherwise, an error occurred, so execute the error callback
} else {
options.onError();
}
// Call the completion callback
options.onComplete();
// Clean up after ourselves, to avoid memory leaks
xml = null;
}
};
// Establish the connection to the server
xml.send();
// Determine the success of the HTTP response
function httpSuccess(r) {
try {
// If no server status is provided, and we're actually
// requesting a local file, then it was successful
return !r.status && location.protocol == "file:" ||
// Any status in the 200 range is good
( r.status >= 200 && r.status < 300 ) ||
// Successful if the document has not been modified
r.status == 304 ||
// Safari returns an empty status if the file has not been modified
navigator.userAgent.indexOf("Safari") >= 0 && typeof r.status == "undefined";
} catch(e){}
// If checking the status failed, then assume that the request failed too
return false;
}
// Extract the correct data from the HTTP response
function httpData(r,type) {
// Get the content-type header
var ct = r.getResponseHeader("content-type");
// If no default type was provided, determine if some
// form of XML was returned from the server
var data = !type && ct && ct.indexOf("xml") >= 0;
// Get the XML Document object if XML was returned from
// the server, otherwise return the text contents returned by the server
data = type == "xml" || data ? r.responseXML : r.responseText;
// If the specified type is "script", execute the returned text
// response as if it was JavaScript
if ( type == "script" )
eval.call( window, data );
// Return the response data (either an XML Document or a text string)
return data;
}
}

在同等目录中,我们可以建立一个rss.xml文件,用这个函数来访问。
rss.xml如下:
复制代码 代码如下:

<titles>
<title>
缘份
</title>
<title>
月亮
</title>
<title>
缘份月亮
</title>
</titles>

再建立一个html文档,调用它,就能看到rss.xml中的内容就能被访问到。
整体看看,其实真的比较简洁和简单。不仅是能访问xml格式文件,html,.js格式的文件都可以调用的;
这些都可以在本地建立对应的文件,进行调用,都可以实现。
更多精彩内容其他人还在看

AJAX 客户端响应速度提高分析

AJAX的出现极大的改变了Web应用客户端的操作模式,它使的用户可以在全心工作时不必频繁的忍受那令人厌恶的页面刷新。
收藏 0 赞 0 分享

揭开AJAX神秘的面纱(AJAX个人学习笔记)第1/5页

写这个学习笔记,只是记载一下自己的学习经过和体会,把一些学习重点记录下来,以备今后的巩固复习及应用,很多知识点没有详细介绍,所以并不完全适用于初学者,如果你是初学者,最好选择一本AJAX学习的书籍,然后与这篇学习笔记对照学习,效果会更好。
收藏 0 赞 0 分享

Ajax Control Toolkit 34个服务器端控件第1/2页

Ajax Control Toolkit 34个服务器端控件,想要学习ajax的朋友可以参考下。
收藏 0 赞 0 分享

Ajax 核心框架函数及例子

最近学习js,肯定会学到ajax中的东西,所以,看到比较好的ajax函数,免不得要贴出来,供大家参考。这个函数摘录自john resig的书中。
收藏 0 赞 0 分享

AJAX 进度条实现代码

AJAX 进度条实现代码,基于java后来,大家可以学习下。
收藏 0 赞 0 分享

一款经典的ajax登录页面 后台asp.net

众所周知,用服务器控件做页面的登录窗体时很简单的,但是页面的多次回传让我们感觉到头痛,一直刷新页面的感觉非常之不好,其实用ajax的局部刷新功能可以完全解决这个问题,制作出来的页面有很好的交互性,而且是局部刷新,节省网络资源。
收藏 0 赞 0 分享

ajax 调用后台方法大家可以讨论下

我曾使用过的三种调用后台的代码,需要的朋友可以参考下,如果发现更好的可以留言。
收藏 0 赞 0 分享

5款Ajax 文件上传控件

如果你的网站含有文件上传功能,那可以使用本文介绍的5款Ajax文件上传控件,提升用户体验。要知道,上传文件总是个痛苦的过程,要消除不太现实,但至少如果你为减少用户的痛苦努力了,那用户也会喜欢你的网站的。
收藏 0 赞 0 分享

AJAX集天气\IP\多国语言翻译MP3(可同步LRC歌词显示)\万年历查询通

AJAX集天气\IP\多国语言翻译MP3(可同步LRC歌词显示)\万年历查询通
收藏 0 赞 0 分享

AJAX 缓存问题的两种解决方法(IE)

ajax 清除缓存的两种方法
收藏 0 赞 0 分享
查看更多