2011-12-26 75 views
28

是否可以在node.js中使用jQuery.ajax(),就像語法上一樣?jQuery.ajax()在node.js中?

我想與node.js共享非UI瀏覽器代碼。我不想用自己的包裝來替換所有現有的函數調用。

目前,當我嘗試它時,默認情況下它會說「無傳輸」,因爲jQuery執行域檢測。如果我通過設置jQuery.support.cors來關閉它,它會說XMLHttpRequest.open()不可用。

+2

用Node中的一個小型HTTP客戶機覆蓋jQuery的Ajax方法將比替換代碼中的所有現有調用更好,但仍然很不方便。 – 2011-12-26 21:58:00

+0

[#380](https://github.com/tmpvar/jsdom/issues/380)引用此問題。剛剛更新了我的[nq](https://npmjs.org/package/nq)軟件包以解決此問題。 – abernier 2012-10-29 16:53:54

回答

35
模塊做控制

我可以使用XMLHttpRequest模塊解決了「無傳輸」的問題,像這樣的:

var $ = require('jquery'), 
    XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; 

$.support.cors = true; 
$.ajaxSettings.xhr = function() { 
    return new XMLHttpRequest(); 
}; 
+0

偉大的工作....那就是解決方案正在尋找:) – 2016-11-23 13:53:23

8

如果你想要的確切jQuery.ajax語法,儘量https://github.com/driverdan/node-XMLHttpRequest

不過說真的,如果你有超過你調用Ajax做什麼的,你應該節點的http.request或類似request

+1

它不像我那麼直觀。 [node jquery](http://search.npmjs.org/#/jquery)模塊內置了對[node-XMLHttpRequest](http://search.npmjs.org/#/xmlhttprequest)的支持。但是,它已被最近的jsdom版本打破。 – voidvector 2011-12-27 06:53:18

+0

@voidvector:你是對的,jsdom現在定義了一個「noop」'XMLHttpRequest'函數,如下所示:https://github.com/tmpvar/jsdom/issues/380 – abernier 2012-10-29 16:56:19

8

也考慮najax,該節點請求模塊的包裝,讓jQuery的風格SYNT斧頭服務器端請求

https://github.com/alanclarke/najax

var najax = require('najax'); 
najax('http://www.google.com', function(html){ console.log(html); }); 
najax('http://www.google.com', { type:'POST' }, function(html){ console.log(html); }); 
najax({ url:'http://www.google.com', type:'POST', success: function(html){ console.log(html); }); 
najax({ url:'http://www.google.com', type:'POST' }).success(function(resp){}).error(function(err){}); 

najax.get, najax.post, najax.put, najax.delete... 
+1

這導致我[jajax](https://npmjs.com/jajax.js)和[djax](https://npmjs.com/djax)沒有依賴關係(特別是龐大的lodash )使它們更適合瀏覽項目。 – 2016-01-14 22:35:13

+0

我最終沒有找到適合我需求的jQuery.ajax等價物。 'djax'不支持非常方便的'complete'處理程序,所以我決定採用未來的技術:https://github.com/github/fetch – 2016-02-05 16:28:46

7

我也是一個瀏覽器和之間的NodeJS共享代碼,並使用JQuery的Ajax調用。 JQuery需要一個我從多米諾骨牌中使用的窗口。

更新:

if (typeof module !== 'undefined' && module.exports) 
{ 
    if (typeof global !== 'undefined' && typeof global.process !== 'undefined' && 
    Object.prototype.toString.call(global.process) === '[object process]') { 
    var domino = require('domino'); 
    var window = domino.createWindow('<html></html>'); 

    var document = window.document; 
    var $ = require('jquery')(window); 
    var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; 
    $.support.cors = true; // cross domain, Cross-origin resource sharing 
    $.ajaxSettings.xhr = function() { 
     return new XMLHttpRequest(); 
    }; 
    } 
} 
+1

爲什麼當它沒有在任何地方定義時導出「huepi」? – HelpMeStackOverflowMyOnlyHope 2016-02-22 04:12:01

+0

好點:)。我從我的項目中使用它複製它,它被稱爲huepi:https://github.com/arndbrugman/huepi。我將從上面的代碼中刪除它。 Thx注意! – 2016-02-23 08:23:59

0

您可以使用Electron,它允許混合browserjs和的NodeJS。

之前,我試過在nodejs中使用canvas2d,但最後我放棄了。它不受nodejs默認支持,並且很難安裝它(許多... dependeces)。直到我使用Electron之前,我可以輕鬆使用我以前的所有browserjs代碼,甚至是WebGL,並將結果值(例如結果base64圖像數據)傳遞給nodejs代碼。