0

I'm gonna start by showing you my code

Angular Code: app.js

$scope.submit = function(){
$scope.jsonData={
                 "status":"OK",
                 "data":{
                        "nbOperatorPresent": $scope.nbOperatorPresent,
                        "objectif1stHour": $scope.objectif1stHour,
                        "objectif2ndHour": $scope.objectif2ndHour,
                        "objectif3rdHour": $scope.objectif3rdHour,
                        "objectif4thHour": $scope.objectif4thHour,
                        "objectif5thHour": $scope.objectif5thHour,
                        "objectif6thHour": $scope.objectif6thHour,
                        "objectif7thHour": $scope.objectif7thHour,
                        "objectif8thHour": $scope.objectif8thHour
                        }
                }
                $http.post("http://localhost:5000/settings",
JSON.stringify($scope.jsonData)).success(function(data,status){
                        console.log('success')
                    })
}

NodeJs Code: index.js

app.post('/settings',urlencodedParser,function(req,res){
         console.log(req.body)
})

As you can see, I have a button to submit the data inserted by the user and send it to the server.

My problem is, when I hit submit, there is nnothing in my browser console, I mean console.log('Success !') didn't work, that is to say that all the code inner the .success(function(data,status)) won't be executed so I can't notify the user that he has submitted Successfully, I don't know where the problem came from !!

BUT In the other console console.log(req.body) I found all the data that has been passed from Angular.

Can anyone explain this to me ? I've tried other solutions but always the same problem :(

4
  • Are you getting any error in console? Of you are using version of angularjs then .success callback is no longer exists you need to use .then functuon. Commented Aug 23, 2017 at 2:47
  • No I don't get any error, but the problem is that the node.js is not returning anything Commented Aug 23, 2017 at 2:49
  • Return something from nodejs backend then. Commented Aug 23, 2017 at 2:53
  • yes yes this is what I did Commented Aug 23, 2017 at 2:55

2 Answers 2

1

To expand on the answer...

Please note if you are unfamiliar with node and express you may want to get in the habit of returning

res.send(success: true, data: res).end()

Its very typical on the angular or UI side to be able to parse as response.data object. Just a suggestion.

and change to this:

.success(function(res,status){
       console.log(res.success, res.data)
 })

This is a very common architecture especially when dealing with web services that you have not control over.

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

Comments

1

You're not returning anything from the node.js code. You need to add in a returning data like:

app.post('/settings',urlencodedParser,function(req,res) {

    console.log(req.body)    
    res.send("Success") 

})

1 Comment

Thank you, I don't know why I didn't notice that

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.