F
F
Fr Zond2019-10-23 13:03:36
Node.js
Fr Zond, 2019-10-23 13:03:36

Receiving ajax request in nodeJS server?

Dear Experts! A question. As a training, I'm trying to send an ajax request from the client to the express server on the local server, but in the console I get empty quotes, that is, the data does not reach (I use axios)


const express = require("express");
        const bodyParser = require("body-parser");
        const app = express();
    
        app.set("view engine", "ejs");
        app.use(express.static("public"));
        app.use(bodyParser.urlencoded({ extended: false}));
    
        app.get("/", (req, res) => {
          res.render("index");
          console.log(req.body);
        });
    
        app.post("/", bodyParser, async (req, res) => {
          console.log(req.body);
          res.render("index");
        });
    
        app.listen(3000);



client code (VUE)
new Vue({
        el: ".app",
        data: { city: null },
        methods: {
          async submit() {
            const response = await axios({
              method: "post",
              url: "/",
              data: {
               body: this.city
              }
            });
          }
        }
      });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Shalaev, 2019-10-31
@Clasen01

This is how you send empty data) you are literally sending null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question