nodejs 运行 出现 TypeError: $.ajax is not a function 错误按照 stackoverflow 的答案做还是出错请问是什么原因?

2020-04-01 17:47:32 +08:00
 woshichuanqilz

我想用 nodejs 运行一个简单网络请求, 代码如下 保存为 test.js 文件

var $ = require("jquery");
var headers = {
  'Accept':'application/json',
  'Authorization':'string'


};


$.ajax({
  url: 'https://api.flaticon.com/v2/total/icons',
  method: 'get',


  headers: headers,
  success: function(data) {
    console.log(JSON.stringify(data));
  }
})

初始化 note js 环境用的命令是 npm init and npm install jqurey

之后运行node test.js 文件出现错误

$ node test.js
d:\python_project\node_test\test.js:10
$.ajax({
  ^
TypeError: $.ajax is not a function
    at Object.<anonymous> (d:\python_project\node_test\test.js:10:3)
    at Module._compile (internal/modules/cjs/loader.js:1151:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

我在 StackOverflow 找到了比较合适的问题 https://stackoverflow.com/questions/18271251/typeerror-ajax-is-not-a-function

里面提示的方法就是 npm instal jquery

但是我这边还是没有成功, 之前 jquery 已经安装过了,

我的文件目录是这样的

─node_modules
    └─jquery
        ├─dist
        ├─external
        │  └─sizzle
        │      └─dist
        └─src
            ├─ajax
            │  └─var
            ├─attributes
            ├─core
            │  └─var
            ├─css
            │  └─var
            ├─data
            │  └─var
            ├─deferred
            ├─effects
            ├─event
            ├─exports
            ├─manipulation
            │  └─var
            ├─queue
            ├─traversing
            │  └─var
            └─var

但不知道错在哪里,所以请教一下

6652 次点击
所在节点    Node.js
10 条回复
zenxds
2020-04-01 17:50:10 +08:00
jQuery 主要还是 DOM 操作,用于浏览器端,运行在 node 环境下可以使用 axios,request 等其他请求库
acthtml
2020-04-01 17:50:25 +08:00
服务端运行不了 jquery.
服务端可用使用 axios
belin520
2020-04-01 17:50:53 +08:00
jquery 可以跑在 nodejs 上?
你直接换成 fetch 就可以了
https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch

fetch('http://example.com/movies.json')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(myJson);
});
zhuangzhuang1988
2020-04-01 17:52:20 +08:00
直接上 axios
VDimos
2020-04-01 18:02:47 +08:00
jquery 可以跑在 nodejs 环境了吗? ajax 在 nodejs 环境有吗?
stillsilly
2020-04-01 18:06:32 +08:00
用内置的 http 模块
https://www.jianshu.com/p/ea0c6ce04cd4

或者用楼上说的 axios 、fetch
airyland
2020-04-01 19:27:06 +08:00
先 console 下 $,看返回什么。
onekkone
2020-04-01 20:40:41 +08:00
jq 的 ajax 是基于 XMLHttpRequest 的 node 里没有这个
SoloCompany
2020-04-01 20:57:49 +08:00
槽点有点多

python_project / node_test
node / jquery

另, jquery 在 node 环境下返回的是一个 factory, 只要你传给这个 factory 一个仿真的 window / document 对象, 则可以得到一个同样仿真的 jquery 实例
woshichuanqilz
2020-04-01 22:09:39 +08:00
不知道是不是因为用了插件的问题, 没法给各位感谢, 这里谢谢各位的帮助, 就是在 node 中没有 jquery 的问题。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/658390

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX