⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ajax.js

📁 Download you own three BR
💻 JS
字号:
function ajax(url, method, callback, data, urlencoded, name) {
	if(url.indexOf('?') > 0)
	{
		url += "&nocache=" + Math.random();
	}
	else
	{
		url += "?nocache=" + Math.random();
	}

	var req, durl, tries = 0;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (typeof name == "undefined") {
		name = "";
	} else {
		name += "\n";
	}
	var readychange = function() {
		if (req.readyState == 4) {// only if req shows "loaded"
			if (req.status < 400) {// only if "OK"
				var lm = null;
				try {
					lm = req.getResponseHeader("Last-Modified");
					lm = new Date(lm);
				} catch (e) { // workaround for Firefox 1.0.7
					lm = new Date();
					if (Math.random() > 0.8) {
						lm -= 2400001;
					}
				}
				method=="POST" ? callback(req) : callback(req,data);
				
			} else if (typeof req == "undefined" || typeof req.status == "undefined") {
				// don't do anything. user has navigated away
			} else if (req.status == 401) { // unauthorized
				callback(req);
			} else if (req.status == 404) {
				if (data && String(data).substr(0,4) == "feed") { 
					// cache miss
					data = "";
					do_request();
				}
			}
		}
	};
	function do_request() {
		if (++tries > 2) { // retry only twice
			return false;
		}
		if (method=="POST") {
			req.open("POST", url, true);
			if (urlencoded) req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			req.onreadystatechange = readychange;
			req.send(data);
		} else {
			durl = url;
			req.open("GET", durl, true);
			req.onreadystatechange = readychange;
			req.send(null);
		}
	};
	do_request();
	return req;
}

function ajaxGet(uri,callback)
{
	return ajax(uri,"GET",callback);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -