How to work with POST,PUT in angularjs

Pradeep J
Kilo Guru

Hi All,

        I have tried with GET method   in $http service(angularjs). Please some one help me to understand how to work with other methods like PUT,POST,JSONP,etc

Thanks

Pradeep

1 ACCEPTED SOLUTION

yes you need headers. in headers I have given access token because you can not make REST calls to servicenow without authenticating first. you can also pass username and password but access token makes more sense when are making REST calls from within servicenow.



and to create incident you need to fill the fields right? I am passing short description.


in ui page I have



<input type="text" ng-model="sd"></input>



so it value in the input field will be available in your UI script(controller) as $scope.sd. which I am passing to POST rest call like this


data:{


  'short_description':$scope.sd


}


View solution in original post

7 REPLIES 7

Rushit Patel2
Tera Guru

Hi Pradeep,



if I remember I explained you how to use $http in other thread. just change the method name from GET to whatever you want. for example see this snippet from my app.



return $http({


                        url: 'https://a.wunderlist.com/api/v1/tasks',


                        method: "POST",


            headers: {'Content-Type': 'application/json,charset=utf-8'},


                        data: {


              list_id :12345,


            title:"my awusme task"


                        }


                          }).then(function(result){


                            //do your logic with result


                        },function(error){


                            //log out error


                        });


so for POST request you need to include one more property called data as shown above. PATCH similar like POST. just change method from POST to PATCH.



return $http({


                      url:https://a.wunderlist.com/api/v1/tasks/taskid?revision_id=1 ,


                      method: "DELETE",


          headers: {'Content-Type': 'application/json,charset=utf-8'}


       


                  });



for delete you don't need to include data. in my example I have specified which task I want to delete in URL as 1234 and there is one parameter called revision id.



it will be different based on different REST APIs.. to delete something in service now you give url something like this


https://dev12985.service-now.com/api/now/table/{tableName}/{sys_id}


 



let me know if you have any doubts.



(please mark correct/helpful/like based on impact).


Hi rushit,


I am trying to post the data which I enter in two textbox.


1.PNG


here is my code:


UI Script



2.PNG



Ui Page



3.PNG



Am I going correct.


Hi Pradeep,


you have given wrong url. and you havnt given any authorization.




see below code. it will create an   incident .


  $scope.createIncident = function(){


      alert('hello create');


        $http({


                method: 'POST',


                url: "https://yourinsta.service-now.com/api/now/table/incident",


          headers: {


    'X-UserToken' : window.g_ck


      },


    data:{


    'short_description':$scope.sd


    }


            }).


            success( function(data, status) {


            $scope.resu = data.result;


            }).


            error ( function(data, status) {


                $scope.error= data.error;


            });


      };


 


I hope this helps


Is that needed to add headers?



And also please explain


data:{


    'short_description':$scope.sd


    }



Can you share your UI Page code also