0

I have used REST API for POST request. When i click on addmode() , it will going to display textbox and on click of save() button the data should get posted. But when i click save() , I am getting error call and re-directing to PUT Request.

Angular Js :

         $scope.addmode = function(information) {
            var postinfo = information;

           $http({
                url:'http://localhost:3000/contacts' , 
                method : 'POST',
                data : postinfo
            })
            .then(
                function successCallback(response) {
                    $scope.selectedcontact = '';
                    console.log(response.data)
                },
                function errorCallback(response) {
                    console.log("Error : " + response.data);
                });    
            };   
vipe
  • 231
  • 2
  • 13

1 Answers1

0

PUT and POST are similar they can both be used for creating, only difference is that PUT should be used for creating and overwriting and POST for modifying/updating. From the looks of it server is requesting PUT why don't you just go with the PUT instead of POST. Also can you post here bit more info about response and error, would be useful.

There's really good answer about PUT/POST here: PUT vs. POST in REST

pegla
  • 1,866
  • 4
  • 17
  • 20
  • what if the user want to post an new information. Then what he should do ?? @pegla – vipe Sep 22 '17 at 07:42
  • Both of them would work, I'm saying that if your backend requires PUT, just use PUT and not POST. Also you never shared your response error and server api, that could be really helpful in this case and some background on server. – pegla Sep 22 '17 at 07:46
  • What you said is right. But currently i need both off them @pegla – vipe Sep 22 '17 at 07:58
  • What I'm saying here is that we can't help you without more information, and we can only guess, my guess is you should change it on server side and make API accept both POST and PUT – pegla Sep 22 '17 at 08:00