2010-08-12 21 views
25

我正在尋找一個非常小的(單線程)Ajax JavaScript庫,以在小腳本的第一行添加一些請求。小型Ajax JavaScript庫

我已經嘗試過:

但他們並沒有在所有的工作。備擇方案?

+2

我看不出有什麼真正原因這兩個庫是行不通的。 – 2010-08-12 18:46:02

+0

可能重複[如何使ajax調用沒有jQuery?](http://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery) – parvus 2013-11-27 10:00:41

+0

我甚至使用microajax中我的大型網站,但可能會轉移到jquery http://static.lastdates.com/package/microajax/test.html – 2014-09-13 03:35:39

回答

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

更新:

爲了做到跨域腳本,你必須打電話出到本地服務器端代理(其讀取和Echo的遠程數據),或者,如果遠程服務返回JSON,使用這種方法:

由於JSON基本上是JavaScript對象或數組,這是一個有效的來源。理論上你應該能夠直接調用遠程服務。我沒有測試過這一點,但它似乎是一個公認的慣例:

參考:Calling Cross Domain Web Services in AJAX

+0

哦,但現在我忘了:它應該是跨域:O – TomShreds 2010-08-12 19:23:42

+0

哦,這是一個粗糙的。 [研究] – 2010-08-12 19:24:47

+3

@Tom我會說,這是值得自己的問題。 – 2010-08-12 19:28:47

-1

那麼...... jQuery可能比你想要的還要大,但它可以說仍然是一個非常好的選擇。它是有據可查的,很好的支持,如果你使用CDN鏈接

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

它甚至很可能存在,並且已經緩存在客戶端的機器上。

+0

Google CDN:http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery .min.js – Radu 2010-08-12 18:48:33

+0

@Radu歡呼,那個URL就是我的意思。 – 2010-08-12 18:50:39

+0

謝謝,但我只想要一個小片段。我使用jQuery處理所有與javascript相關的工作,但是這次我需要的東西很小。 – TomShreds 2010-08-12 18:55:01

3

所以... ...微小

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

很多ajax。這樣的哇。 – samvv 2016-05-08 20:27:03

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

可以probab使用omee。它是一個單獨的文件,其中包含許多常用的javascript函數,如ajax請求。

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

提高一個Ajax請求你只需要調用 omee.raiseAjaxRequest

與參數

params-參數列表即摹參數1 = param1value &參數2 = param2value

網址 - 網址擊中服務器

FUNC-功能名稱將被稱爲回

connType - GET/POST。

19

您可以構建自己的jQuery版本,該版本只包含AJAX模塊。

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

+0

這是非常好的建議。誰會低估它,爲什麼?去SE !. – Phil 2013-11-16 19:32:14

+1

感謝@phil,一個心懷不滿的情人,也許 – msaspence 2014-01-17 16:39:27

+2

http://projects.jga.me/jquery-builder/建議即使是ajax只jQuery 2.1.1是18kb gzipped和縮小(完整的jQuery是28kb)。只是想提到這一點,因爲我驚訝它並不小。 – Keeth 2015-02-13 19:02:51