小型 Ajax JavaScript 库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3470895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Small Ajax JavaScript library
提问by Tommy B.
回答by Ryan Kinal
Here you go, pretty simple:
给你,很简单:
function createXHR()
{
var xhr;
if (window.ActiveXObject)
{
try
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert(e.message);
xhr = null;
}
}
else
{
xhr = new XMLHttpRequest();
}
return xhr;
}
Documentation is here
文档在这里
Example:
例子:
var xhr = createXHR();
xhr.onreadystatechange = function()
{
if (xhr.readyState === 4)
{
alert(xhr.responseText);
}
}
xhr.open('GET', 'test.txt', true)
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send()
Update:
更新:
In order to do cross-domain scripting, you'll either have to call out to a local server-side proxy (which reads and echo's the remote data), or, if your remote service returns JSON, use this method:
为了执行跨域脚本,您必须调用本地服务器端代理(读取并回显远程数据),或者,如果您的远程服务返回 JSON,请使用以下方法:
var s = document.createElement('script')
s.src = 'remotewebservice.json';
document.body.appendChild(s);
Since JSON is essentially a JavaScript object or array, this is a valid source. You theoreticallyshould then be able to call the remote service directly. I haven't tested this, but it seems to be an accepted practice:
由于 JSON 本质上是一个 JavaScript 对象或数组,因此这是一个有效的来源。理论上,您应该能够直接调用远程服务。我还没有测试过这个,但这似乎是一种公认的做法:
Reference: Calling Cross Domain Web Services in AJAX
回答by msaspence
You can build your own version of jQuery that only includes the AJAX modules.
您可以构建自己的仅包含 AJAX 模块的 jQuery 版本。
https://github.com/jquery/jquery#how-to-build-your-own-jquery
https://github.com/jquery/jquery#modules
https://github.com/jquery/jquery#how-to-build-your-own-jquery
https://github.com/jquery/jquery#modules
回答by Rion
So...tiny...
那么……小……
var obj = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : (XMLHttpRequest && new XMLHttpRequest()) || null;
回答by Shimon Doodkin
Here is my version with async callback in node.js style
这是我的 node.js 风格异步回调版本
https://gist.github.com/4706967
https://gist.github.com/4706967
// tinyxhr by Shimon Doodkin - licanse: public doamin - https://gist.github.com/4706967
//
// tinyxhr("site.com/ajaxaction",function (err,data,xhr){ if (err) console.log("goterr ",err,'status='+xhr.status); console.log(data) });
// tinyxhr("site.com/ajaxaction",function (err,data,xhr){ if (err) console.log("goterr ",err,'status='+xhr.status); console.log(data) },'POST','value1=1&value2=2');
// tinyxhr("site.com/ajaxaction.json",function (err,data,xhr){ if (err) console.log("goterr ",err,'status='+xhr.status); console.log(data); console.log(JSON.parse(data)) },'POST',JSON.stringify({value:1}),'application/javascript');
// cb - function (err,data,XMLHttpRequestObject){ if (err) throw err; }
//
function tinyxhr(url,cb,method,post,contenttype)
{
var requestTimeout,xhr;
try{ xhr = new XMLHttpRequest(); }catch(e){
try{ xhr = new ActiveXObject("Msxml2.XMLHTTP"); }catch (e){
if(console)console.log("tinyxhr: XMLHttpRequest not supported");
return null;
}
}
requestTimeout = setTimeout(function() {xhr.abort(); cb(new Error("tinyxhr: aborted by a timeout"), "",xhr); }, 5000);
xhr.onreadystatechange = function()
{
if (xhr.readyState != 4) return;
clearTimeout(requestTimeout);
cb(xhr.status != 200?new Error("tinyxhr: server respnse status is "+xhr.status):false, xhr.responseText,xhr);
}
xhr.open(method?method.toUpperCase():"GET", url, true);
//xhr.withCredentials = true;
if(!post)
xhr.send();
else
{
xhr.setRequestHeader('Content-type', contenttype?contenttype:'application/x-www-form-urlencoded');
xhr.send(post)
}
}
tinyxhr("/test",function (err,data,xhr){ if (err) console.log("goterr ",err); console.log(data) });
回答by agaase
You can probably use omee. Its a single file containing many frequently used javascript functions like ajax request.
您可能可以使用 omee。它的单个文件包含许多常用的 javascript 函数,如 ajax 请求。
https://github.com/agaase/omee/blob/master/src/omee.js
https://github.com/agaase/omee/blob/master/src/omee.js
to raise an ajax request you just call omee.raiseAjaxRequest
要提出 ajax 请求,您只需调用 omee.raiseAjaxRequest
with arguments
有论据
params- parameters list e.g param1=param1value¶m2=param2value
params- 参数列表 例如 param1=param1value¶m2=param2value
url - url to hit the server
url - 访问服务器的 url
func- function name which is to be called back
func- 被回调的函数名
connType - GET/POST.
connType - GET/POST。
回答by Pekka
Well...... jQueryis probably bigger than what you want, but it's arguably still a very good option. It's well documented, well supported, and if you use the CDN link
嗯...... jQuery可能比你想要的要大,但可以说它仍然是一个非常好的选择。它有据可查,得到很好的支持,如果您使用 CDN 链接
http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js
it is even very likely to be present and cached on the client's machine already.
它甚至很可能已经存在并缓存在客户端的机器上。

