小型Ajax JavaScript库

时间:2010-08-12 18:41:40

标签: javascript ajax

我正在寻找一个非常小的(一个线程)Ajax JavaScript库来添加一个小脚本的第一行来发出一些请求。

我已经尝试过:

但他们根本不工作。替代?

6 个答案:

答案 0 :(得分:33)

在这里,非常简单:

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;
}

文档为here

示例:

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()

<强>更新

要进行跨域脚本编写,您必须调用本地服务器端代理(读取和回显远程数据),或者,如果远程服务返回JSON,则使用此方法:

var s = document.createElement('script')
s.src = 'remotewebservice.json';
document.body.appendChild(s);

由于JSON本质上是一个JavaScript对象或数组,因此这是一个有效的源。您理论上应该能够直接调用远程服务。我没有对此进行过测试,但这似乎是一种公认​​的做法:

参考:Calling Cross Domain Web Services in AJAX

答案 1 :(得分:20)

答案 2 :(得分:3)

所以... ...微小

var obj = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : (XMLHttpRequest && new XMLHttpRequest()) || null;

答案 3 :(得分:3)

这是我在node.js样式中使用异步回调的版本

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)  });

答案 4 :(得分:0)

你可以使用omee。它是一个单独的文件,包含许多常用的javascript函数,如ajax请求。

https://github.com/agaase/omee/blob/master/src/omee.js

提出你刚才打电话的ajax请求 omee.raiseAjaxRequest

带参数

params-参数列表,例如param1 = param1value&amp; param2 = param2value

url - 要点击服务器的网址

要回叫的函数名称

connType - GET / POST。

答案 5 :(得分:-1)

嗯...... jQuery可能比你想要的要大,但它可能仍然是一个非常好的选择。它有很好的文档,很好的支持,如果你使用CDN链接

http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

它甚至很可能已经存在并缓存在客户端的机器上。

相关问题