ajax requests to Node.js
NickName:user8463989 Ask DateTime:2022-07-28T01:45:57

ajax requests to Node.js

In my app.js file in Node.js I have this:

app.use(express.json()); 

which I thought was good enough but whenever I submit data via jQuery/Ajax the response is an empty object.

After doing the below however, it now get the data from req.body. But, I thought that in the newer versions of Express it was not necessary to do this anymore?

const bodyParser = require("body-parser");

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

The Ajax request:

    $.ajax({
        url: 'http://localhost:8000/api/v1/endpoint',
        method: 'POST',
        data: {key: key}
    })
    .done(function(data) {
        console.log(data)
    })
    .fail(function(data) {
        console.log(data);
    })

Copyright Notice:Content Author:「user8463989」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/73142483/ajax-requests-to-node-js

More about “ajax requests to Node.js” related questions

What is the limit of sending concurrent ajax requests with node.js?

I am making a node.js server that makes a lot of ajax requests to download json or html code, to different websites (not ddos/dos). For this language, I was wondering how many requests I can make a...

Show Detail

Multiple Ajax Requests to Node.js timing out

I have a very unique setup, which I imagine is having some effect on this issue (which I've never seen before). I'm writing a Google Chrome Extension, and I'm relying on Node.js as my backend. Wh...

Show Detail

Node.js server not receiving AJAX requests?

I'm using Express and Node.jx 0.6.7. Server is EC2 Small instance.. For some reason, Node.js is sometimes not receiving my ajax requests (sometimes). Firebug would say "pending..." and the spinn...

Show Detail

Node.js: Sending many AJAX requests to begin backend job queues

Preface: I'm trying to prefetch content from a given set of URLs asynchronously. I'm needing to send my node.js app around 40-60 local ajax requests in order to add jobs to a queue (node-chain-ga...

Show Detail

Scrape a page with AJAX requests

I'm using Node.js with jsDOM to scrape web pages and execute JavaScript in them. But if a page contains AJAX requests, they are not executed. Is there an option to execute AJAX calls inside the sc...

Show Detail

Node.js Asynchrounous requests

I have an application which shows real-time prices of different markets to users. I tried ajax and WebSockets both node.js takes all requests synchronously which delays the data showing on the user

Show Detail

Ajax requests with Node.js

After looking around google and stack overflow, I'm yet to find a way to post data to my Node.js server. There seems to be disparity between the methods I've seen, presumably due to depreciation. ...

Show Detail

Replacing JavaScript/PHP ajax request with node.js

I'm having a web application which is using PHP/MySQL pair. Now i like to replace only the AJAX requests to node.js to reduce the latency to less than 1 second. So when i googled about this, i came...

Show Detail

Change the Node.js GET /POST requests to the Ajax

I'm a beginner in JavaScript and working to make the same request from some Node.js backend code to by using the Ajax based GET/ POST requests. I use MySQL database. I have gone through some tutori...

Show Detail

Node.js: event-based comet insted of periodic ajax requests?

Core features of node.js as far as I understood are (also): event-based structure and very cheap "threads" sleeping. So, it may be really good idea (and probably cheaper) to do not create periodic ...

Show Detail