0
I’m completely new working with nodejs, I’ve seen some questions similar to mine but I couldn’t, and I didn’t understand straight would have as someone give me a help?
In case I am trying to send an ajax request using the post method with nodejs.
This is my ajax code:
$.ajax({
url: "/components/config/action-send-plano",
data: { field1: 'valor 001##', field2: 'valor 002##' },
type: 'POST',
contentType: false, // NEEDED, DON'T OMIT THIS (requires jQuery 1.6+)
processData: false, // NEEDED, DON'T OMIT THIS
beforeSend: function () {
$("#" + formName).html(preloaderAzul);
},
success: function (response) {
$("#" + formName).html(response);
}
});
and this is the route to which he sends the request
const express = require('express')
const app = express()
const path = require('path')
const handlebars = require('express-handlebars')
// Config
// Template Engine
app.engine('handlebars', handlebars({ defaultLayout: 'main' }))
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'handlebars')
// Add files
app.use(express.static(__dirname + "/"))
// Body Parser
const bodyParser = require("body-parser")
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
// Routes
app.get('/', (req, res) => {
res.render('landingpage')
})
app.post('/components/config/action-send-plano', function (req, res, next) {
console.log(req.body)
})
In this case, it sends, but I cannot receive the values of field1 and field2, the console returns me all the data referring to the req parameter, but the Fields result does not appear
Is using the body-parser?
– KaduAmaral
Oops, so I am
– Pedro Hygor
Opa I think Taffarel already helped you, just an observation, you don’t need to use both
app.use(express.json())
andapp.use(bodyParser.json())
Only one of the two I think is enough. I haven’t touched Node in a few months, so look at the documentation which one is most up to date and just use it. The same goes forurlencoded
.– KaduAmaral