I created the route below to accept POST requests, but calls via AJAX for this route are always returning error 404: Not Found.
/* Arquivo: integracoes.js */
var express = require('express');
var router = express.Router();
// Esta rota funciona e não dá erro: http://localhost:3001/integracoes/consultas
router.get('/consultas', function(req, res, next) {
res.render('consultas');
});
// 404: Not Found quando chamado pela requisição Ajax descrita mais à frente nessa pergunta.
router.post('/consulta/statuspedido', function(req, res) {
var statusDoPedido = 10;
res.send(JSON.stringify({statuspedido: statuspedido}));
});
module.exports = router;
In app.js I make use of this route definition as follows:
var integracoes = require('./routes/integracoes');
app.use('/integracoes', integracoes);
In the HTML page I'm using the following Ajax call:
$.ajax({
url: '/consulta/statuspedido',
contentType: 'application/json',
type: 'POST',
success: function(data) {
debugger;
textAreaDeResposta.val(imprimaCamposDoObjeto(data));
},
error: function(){
textAreaDeResposta.val('Ocorreu um erro ao tentar consultar o status do pedido.');
}
});
In the Browser the response I get is as follows:
link 404 (Not Found)
The site is configured to run on port 3001, and all other site (GET) routes are running. The only one that does not work is the one I'm trying to access via AJAX.
Why is the URL not found?