0

The body parser body is {}. I've already done research and made sure that my ajax data key is set correctly as well as make sure the middleware is set up correctly as well. Here is my frontend ajax call

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

And here is my 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 at http://%s:%s', host, port);
});
Edward Hu
  • 57
  • 1
  • 8

1 Answers1

0

You are using a GET request, so it's probably not being sent. If you need to send something, you can attach it as a header or include it in the query string of the url. If you want to send data, I would use a POST request.

Check out this article

How to send data in request body with a GET when using jQuery $.ajax()

Community
  • 1
  • 1
Dustin Stiles
  • 1,414
  • 9
  • 12