javascript - 如何在 node.js 中运行 jQuery

标签 javascript jquery json node.js

<分区>

我已经尝试过 npm install jQuery --save 并在我的 Node 文件中 var $ = require('jquery'); 但是,当我运行我的 Node 文件时与

$.getJSON('https://en.wikipedia.org/wiki/Washington,_D.C.', function(data) {
    //data is the JSON string
});

我得到了错误

TypeError: $.getJSON is not a function
    at Object.<anonymous> (C:\Users\Karim\node 2\tweetPic.js:16:3)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

我也尝试过使用

导入 jquery
require("jsdom").env("", function(err, window) {
if (err) {
    console.error(err);
    return;
}

var $ = require("jquery")(window);
});

刚刚返回

TypeError: require(...).env is not a function
    at Object.<anonymous> (C:\Users\Karim\node 2\tweetPic.js:3:18)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

我以类似的方式安装了 jsdom 包。我的 jquery 代码本身有问题吗?我该如何解决这个问题?

编辑:看来 jQuery 并不是我真正需要的。我将研究一种不同的检索 json 数据的方法。

最佳答案

抱歉,我不知道如何安装 jquery。但是您显然需要它来请求网站并获取 json。你可以使用 request为了那个原因。希望我能帮到你。

你可以这样做:

request('https://en.wikipedia.org/wiki/Washington,_D.C.', function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // Print the HTML for the Google homepage.
});

关于javascript - 如何在 node.js 中运行 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48993590/

相关文章:

javascript - 将变量插入 jQuery .css

javascript - 从谷歌表格获取数据适用于 jsfiddle 但不适用于本地客户端

jquery - 如何使用 JQuery+JEditable 一次编辑/保存多个字段?

jQuery 在显示/隐藏之间缓动动画

php - 获取 JSON 对象的第一个条目

java - 如何使用 Jackson 将 JSON 字符串解析为数组

iOS swift 将 JSON 分离成数组

javascript - 获取一个点从另一个点旋转一定距离的X和Y

javascript - HTTP 与 HTTPS 有什么区别,SSL 到底发生了什么?

javascript - 选择框更改时传递值