简体   繁体   English

在Node.js项目中使用jquery进行api请求

[英]Using jquery in a Node.js project to make api request

i am trying to make an api request in a node project using jquery, which I've istalled via npm install jquery but I'm getting the error TypeError: $.post is not a function even after requiring jquery like so: const $ = require('jquery') how can I get Jquery to work? 我正在尝试使用jquery在节点项目中提出api请求,我通过npm install jquery但是我遇到了错误TypeError: $.post is not a function甚至TypeError: $.post is not a function在需要jquery之后TypeError: $.post is not a functionconst $ = require('jquery')如何让Jquery工作? I've seen a lot of examples of people using jquery but the process was not clear to me since I'm new to Node.js Here is my code 我已经看到很多使用jquery的人的例子,但是由于我是Node.js的新手,所以这个过程对我来说并不明确,这是我的代码

const $ = require('jquery')

$.post("https://ticketsoko.nouveta.co.ke/api/index.php", {
    TransactionType: "getEvents"
}, function (data, status) {
    var info = JSON.parse(data);
    for (var i = 0; i < info.data.length; i++) {
        ticketSales[i] = info.data[i].totalTicketsSalesAmount;
        events[i] = info.data[i].Events.events_name;
        console.log(events[i]);
    }
})

Here is step by step hard-to-go-wrong tutorial on how to obtain jQuery into your NodeJs project, then require it in your code and use in a manner similar to what you would do without NodeJs. 这是关于如何将jQuery导入到NodeJs项目中,然后在代码中要求使用jQuery以及如何使用类似于不使用NodeJs时类似的方式的逐步教程。 If you haven't heard of modules and npm and are working with NodeJs, YESTERDAY was the time to have some working knowledge with them. 如果您还没有听说过模块和npm并正在使用NodeJ,那么YESTERDAY是时候与他们一起了解一些工作知识了。

click here to see how to 点击这里查看如何

On the other hand, WHY are you using jQ for this? 另一方面,为什么要为此使用jQ? You have entire world of modules to your disposal via npm, there are better modules, more fitted to do HTTP requests with like this one: 您可以通过npm处理整个模块,还有更好的模块,更适合执行HTTP请求,如下所示:

request module 请求模块

You have to use a capital Q for jQuery 您必须对jQuery使用大写Q

So it is const $ = require('jQuery') instead of require('jquery') . 因此它是const $ = require('jQuery')而不是require('jquery')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM