为什么jQuery包中没有用于node.js的$ .ajax方法? [英] Why is there no $.ajax method in jQuery package for node.js?

查看:167
本文介绍了为什么jQuery包中没有用于node.js的$ .ajax方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下所示,我想使用jQuery的ajax函数进行练习(我已经运行npm install jquery来安装软件包):

My code looks like this, I want to practice with jQuery's ajax function ( I have already run npm install jquery to install the package):

var $ = require('jquery');

var remoteValue = false;

var doSomethingWithRemoteValue = function() {
    console.log(remoteValue); 
}

var promise = $.ajax({
    url: 'https://google.com'
});

//outputs "true"
promise.always(function() {
    remoteValue = true;
    doSomethingWithRemoteValue();    
});

//outputs "foobar"
promise.always(function() {
    remoteValue = 'foobar';
    doSomethingWithRemoteValue();    
});

但是NodeJS编译器抱怨jQuery模块中没有ajax方法.

But the NodeJS compiler complains that there is no ajax method in jQuery module.

bash-3.2$ node test.js 

/Users/hanfeisun/Downloads/node/test.js:14
var promise = $.ajax({
                ^
TypeError: Object function ( w ) {
                if ( !w.document ) {
                    throw new Error( "jQuery requires a window with a document" );
                }
                return factory( w );
            } has no method 'ajax'
    at Object.<anonymous> (/Users/hanfeisun/Downloads/node/test.js:14:17)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

用于浏览器JS编译器和NodeJS的jQuery的javascript代码不应该吗?

Shouldn't the javascript codes of jQuery for Browser JS Compiler and NodeJS the same?

如果没有,是否有人知道主要原因?谢谢!

If not, does anyone have idea about the main reason? Thanks!

推荐答案

我对Node的javascript环境不太熟悉,但是ajax的要点是使用javascript处理服务器响应而不会导致页面刷新.在运行Node的服务器上,您将只能发出普通的HTTP请求.

I'm not too familiar with Node's javascript environment, but the point of ajax is to handle a server response with javascript without causing a page refresh. On a server running Node, you would just be able to make a normal HTTP request.

实际上,谷歌的快速搜索显示JQuery使用$ .ajax构建的基础XMLHttpRquest在Node中不存在(实际上,

In fact, a quick google search shows that the underlying XMLHttpRquest that JQuery builds on with $.ajax doesn't exist in Node (and in fact the Wikipedia article on XHR suggests it's part of the Browser object model), so it wouldn't be available from Node (though you can probably emulate it by leveraging Node's http request module.)

如果要尝试$ .ajax,则应在浏览器环境中进行.您可以从任何包含JQuery的网页(例如Stackoverflow)的javascript控制台中执行此操作,也可以在带有从CDN中获取JQuery的标头中的Script标记来制作自己的测试HTML页面.

If you want to experiment with $.ajax, you should do it within a browser environment. You could do it from the javascript console from any webpage that includes JQuery, such as Stackoverflow, or make your own test HTML page with a Script tag in the header that sources JQuery from a CDN.

这篇关于为什么jQuery包中没有用于node.js的$ .ajax方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆