2

I'm trying to post data from angular to express, however, eachtime I make the post request I get this error.

OPTIONS http://localhost/post/ net::ERR_CONNECTION_REFUSED(anonymous function) @ angular.js:11209s @ angular.js:11002g @ angular.js:10712(anonymous function) @ angular.js:15287m.$eval @ angular.js:16554m.$digest @ angular.js:16372m.$apply @ angular.js:16662(anonymous function) @ angular.js:24283m.event.dispatch @ jquery.js:4670r.handle @ jquery.js:4338
controller.js:29 

and the errorCallback response object is:

Object {data: null, status: -1, config: Object, statusText: ""} 

I did some tweaking looking at other similar questions on SO, like adding header, content-type etc. But no luck there.

What seems to be the problem here. Thanks

Controller.js

word.add = function(){
console.log(word.name );

    $http({
        method:'POST',
        url:'http://localhost/post/',
        data :word.name,
        headers: {'Content-Type': 'application/json'} 
    }).then(function successCallback(response){
        console.log(response);
        console.log("Successful");
    },function errorCallback(response){

        console.log(response);
        console.log("Unsuccessful");
    });     
};

Relevant code excerpt from app.js

 var routes = require('./routes/index');
    app.all('/*', function (request, response, next) {
       response.header("Access-Control-Allow-Origin", "*");
       response.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
        response.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
  next();
});

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/post', routes);

index.js

var express = require('express');
var router = express.Router();
router.post('/post', function(request, response){
    console.log(request.body.name);
    response.send('Reached post');
});

module.exports = router;

Edit: Hi, I've already tried and added the content-type header in my response. Still,It's the same error.

The same code works fine while I use this method to post.

$http.post('/post',{data: word.name}).success(function(response) {
       console.log("success");
      }).error(function(err){
         console.log("failure")
      });
7
  • Possible duplicate of CORS trouble with nodejs and AngularJS Commented Dec 14, 2015 at 2:49
  • A preflight is fired. That means CORS is in place. You can either make your client and server to be in same domain or add CORS headers. Commented Dec 14, 2015 at 2:50
  • @Phil I've looked into that question, and added the CORS, still facing the same error. I'm not sure if it's related to content-type header error. Commented Dec 14, 2015 at 3:06
  • Well, net::ERR_CONNECTION_REFUSED makes me think your Express server isn't actually running (or at least, not running on port 80) Commented Dec 14, 2015 at 4:12
  • Hi, One weird behavior I encountered is, I'm able to post data successfully while using this method. $http.post('/post',{data: word.name}).success(function(response) { console.log("success"); }).error(function(err){ console.log("failure") }); Commented Dec 14, 2015 at 4:27

2 Answers 2

0

expressjs listing at 9000 port by default. So, you post url should be http://localhost:9000/post/

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

Comments

0

You must enable CORS on your server. Since the port is different on the same server it will treat it like a cross domain request.

http://enable-cors.org/server_expressjs.html

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.