node.js - BodyParser data is undefined after AJAX call -


the body parser body {}. i've done research , made sure ajax data key set correctly make sure middleware set correctly well. here frontend ajax call

$.ajax({ type:"get", url:"/api", data: {course:"math-226"}, success: function(data){ alert(data);} });

and here backend server.js file:

'use strict' const express = require('express'); const path = require('path'); const bodyparser = require('body-parser'); const alg = require('./app/algorithm.js'); const app = express(); app.use('/', express.static(path.join(__dirname, 'public'))); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({extended: true}));  app.get('/api', (req, res) => { console.log(req.body); alg.create(req.body.course, answer => res.send(answer)); }); let server = app.listen(3000, () => {   let host = server.address().address;   let port = server.address().port;   console.log('example app listening @ http://%s:%s', host, port); }); 

you using request, it's not being sent. if need send something, can attach header or include in query string of url. if want send data, use post request.

check out article

how send data in request body when using jquery $.ajax()


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -