Eoin Murray
Eoin Murray

Reputation: 1955

expressjs POST response not working

Maybe it's something simple but the below code isn't working, the expressjs server is receiving the request and data, but the jQuery ajax call isn't receiving the response from the server

this is the server code

 app.get('/', function(req, res){
  console.log('request for /');
  res.send('hello there!');    
 });

this is the jQuery request

  $.ajax({
    type: 'GET',
    url: 'http://localhost:3000/',
    success: function(data) {
        alert(data);
    }
  });

this is the expressjs app configurations

 app.configure(function(){
  app.set('port', /*process.env.PORT ||*/ 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
 });  

EDIT: took out Json stuff, problem exists anyway

Upvotes: 1

Views: 593

Answers (2)

Eoin Murray
Eoin Murray

Reputation: 1955

Solution was to allow cross domain headers

      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Headers", "X-Requested-With"); 

Upvotes: 2

Hitesh Chavda
Hitesh Chavda

Reputation: 789

The problem is you did not end the connection in server code as below. In app.get('/', ...

res.end('hello there');

Note that end instead of send.

Upvotes: 0

Related Questions