0

I am building my website with Angularjs and I am testing it with Node js and express. So, I am able to send a json item with information from the contact page form. But, I do not know How to return a success or fail code from Node.js to Angularjs to redirect to a thank you page. Also, I need to know how to send the information to my email and save it to a db and how to add a token for the form. I know that I'm using the MEAN stack. I learn better watch.

Here is my Node js:

var express = require('express');
var app = express();
var formidable = require('formidable');

app.use(express.static(__dirname + '/public'));    // navigate to where the app reside

app.get('/', function (request, response) {
    response.redirect('Index.html');
});

app.post('/Contact', function (request, response) {
    var frm = new formidable.IncomingForm();
    frm.parse(request, function(err, formData){
    var Name = formData.Name,
        Email= formData.Email,
        Message = formData.Message;
        response.writeHead(200, { "Content-Type": "application/json" });
        response.end("{'status:' 200}");
    });
});
var port = 8080;
app.listen(port);
console.log('Listening on port:  ' + port);

and here is my Angularjs:

$scope.submit = function () {
        console.log('Im in the controller');
        console.log($scope.formData);       
        $http({
            method  : 'POST',
            url     : '/Contact',
            data    : $.param($scope.formData), 
            headers : { 'Content-Type': 'application/json' } 
        }).success(function(result, status, headers, config) {
                console.log(result);
            if(result.status == "???"){
                redirectTo: '/Thnkyu';
            }
        }).error(function(result, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
            console.log(result);
        });
    }
3
  • 1
    result.status == "200", doesn't work?? Commented Jan 8, 2015 at 7:38
  • response.json({ status: 200}); Commented Jan 8, 2015 at 8:04
  • When I submit the form; the console shows: TypeError: Cannot read property 'protocol' of undefined Before it showed: ERR_CONNECTION_REFUSED because I had the url portion before I seted the headers and data. Commented Jan 10, 2015 at 21:29

1 Answer 1

1

When I submit the form; the console showed: TypeError: Cannot read property 'protocol' of undefined Before it showed: ERR_CONNECTION_REFUSED because I had the url portion before I setted the headers and data.

The problem was I use the angularJs $http dependence as shown above; but, it did not work. So, I use the $http dependence like so:

$scope.submit = function () {
        console.log($scope.formData);
        $http.post("/contact", $scope.formData)
        .success(function(reponse){
            if(reponse.status == 200)
            console.log('im in')
            $location.path('/thnkyu');
        });

and the response from the server is like this:

...
if(!eor){
        console.log(eor);
        response.json({status: 200});
    }
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.