【问题标题】:Small Ajax JavaScript library [closed]小型 Ajax JavaScript 库 [关闭]
【发布时间】:2010-08-12 18:41:39
【问题描述】:

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

我已经试过了:

但它们根本不起作用。替代品?

【问题讨论】:

标签: javascript ajax


【解决方案1】:

给你,很简单:

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

【讨论】:

  • 哦,但现在我忘记了:它应该是跨域的:O
  • 哦,这很粗糙。 [研究]
  • @Tom 我会说这个问题本身就值得。
  • 更新了一个未经测试的答案:-D
  • 供将来参考,xdomain 是一个非常好的跨域脚本选择。
【解决方案2】:

您可以构建自己的仅包含 AJAX 模块的 jQuery 版本。

https://github.com/jquery/jquery#how-to-build-your-own-jquery
https://github.com/jquery/jquery#modules

【讨论】:

  • 这是一个非常好的建议。谁会反对它,为什么?去SE!。
  • 谢谢@phil,也许是个心怀不满的情人
  • projects.jga.me/jquery-builder 建议即使是仅 ajax 的 jQuery 2.1.1 也是 18kb 压缩和缩小(完整的 jQuery 是 28kb)。只是想提一下,因为我很惊讶它没有变小。
【解决方案3】:

那么……小……

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

【讨论】:

  • 很多 ajax。这样哇。
【解决方案4】:

这是我的 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)  });

【讨论】:

    【解决方案5】:

    您可能可以使用 omee。它是一个包含许多常用 javascript 函数(如 ajax 请求)的单个文件。

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

    提出一个您只需调用的 ajax 请求 omee.raiseAjaxRequest

    带参数

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

    url - 访问服务器的 url

    func-要回调的函数名

    connType - GET/POST。

    【讨论】:

      【解决方案6】:

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

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

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 2014-10-18
      • 2010-11-03
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 2012-09-06
      相关资源
      最近更新 更多