0

In my node js express app, I'm trying get data from database to a ejs file via ajax.

$.ajax({
        type: 'POST',
        data: JSON.stringify(data),
        cache: false,
        contentType: 'application/json',
        datatype: "json",
        url: 'accelData',
        success: function (result) {
          console.log(result);
        }
      });
    }

I have created a route as well.

router.get('/', function(req, res, next) {

    var con = mysql.createConnection({
        host: "localhost",
        user: "root",
        password: "root",
        database: "db_name"
    });

    con.connect(function(err){
        if(err) return;
    });

    con.query('SELECT * FROM table_name',function(err, result){
        if(err) return err;
        var response = {
            data : result
        };
        res.send(response);
    });
    con.end();
});

module.exports = router;

and I added the route to app.js file.

var accelData = require('./routes/accelData');
app.use('/accelData', accelData);

When I run the node server, ajax request fails with 404 Not Found http://localhost/accelData

But when I try the url in my browser it shows me the data. What is the issue?

1 Answer 1

3

You're sending a POST request to a GET route.

That won't work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.