node.js 中的 jQuery.ajax()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8638820/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:48:41  来源:igfitidea点击:

jQuery.ajax() in node.js?

jquerynode.js

提问by voidvector

Is it possible to use jQuery.ajax()in node.js exactly as it is syntax-wise?

是否可以jQuery.ajax()完全按照语法在 node.js 中使用?

I am trying to share non-UI browser code with node.js. I do not want to replace all the existing function calls with my own wrapper.

我正在尝试与 node.js 共享非 UI 浏览器代码。我不想用我自己的包装器替换所有现有的函数调用。

Currently when I try it, it would say "No Transport" by default because jQuery does domain detection. If I turn it off by setting jQuery.support.corsit would say XMLHttpRequest.open() not available.

目前,当我尝试它时,默认情况下它会说“无传输”,因为 jQuery 会进行域检测。如果我通过设置关闭jQuery.support.cors它会说 XMLHttpRequest.open() 不可用。

回答by webXL

I was able to solve the "No Transport" issue using the XMLHttpRequest module, like this:

我能够使用 XMLHttpRequest 模块解决“无传输”问题,如下所示:

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

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

回答by alzclarke

also consider najax, a wrapper for the node request module which allows jquery style syntax for server-side requests

还可以考虑 najax,它是节点请求模块的包装器,它允许服务器端请求的 jquery 样式语法

https://github.com/alanclarke/najax

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...

回答by fent

If you want the exact jQuery.ajax syntax, try https://github.com/driverdan/node-XMLHttpRequest

如果您想要确切的 jQuery.ajax 语法,请尝试https://github.com/driverdan/node-XMLHttpRequest

But really if you have control over what you're calling ajax for, you should do it with node's http.requestor a module like request

但实际上,如果您可以控制调用 ajax 的目的,则应该使用节点http.request请求之类的模块来执行此操作

回答by Arnd Brugman

I am also sharing code between a browser and nodejs and also use JQuery for Ajax calls. JQuery requires a window which I use from domino.

我还在浏览器和 nodejs 之间共享代码,并且还使用 JQuery 进行 Ajax 调用。JQuery 需要一个我从 domino 中使用的窗口。

update:

更新:

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

回答by gonnavis

You can use Electron, it allows hybrid browserjs and nodejs.

您可以使用Electron,它允许混合 browserjs 和 nodejs。

Before, I tried to use canvas2d in nodejs, but finally I gave up. It's not supported by nodejs default, and too hard to install it (many many ... dependeces). Until I use Electron, I can easily use all my previous browserjs code, even WebGL, and pass the result value(eg. result base64 image data) to nodejs code.

之前尝试在nodejs中使用canvas2d,最后放弃了。nodejs 默认不支持它,而且安装它太难了(很多很多......依赖)。在我使用 Electron 之前,我可以轻松地使用我以前所有的 browserjs 代码,甚至是 WebGL,并将结果值(例如结果 base64 图像数据)传递给 nodejs 代码。