Santhosh Aineri
Santhosh Aineri

Reputation: 559

How to get data from nodejs web service with javascript?

I have a nodejs web service url. Through that I am able to perform authentication. I am using AJAX for that. I want to do the same operation with simple javascript.

This is my service.js code

var LOGIN_AUTHENTICATION = "http://192.168.84.88:8888/AD-Service/rest/getEmpDetails/authenticateUser";

this.getLoginAuthentication = function(taskObject) {
           var promise = null;
           if (!promise) {
               promise = $http({
               method: 'POST',
                url: LOGIN_AUTHENTICATION,
                data: taskObject,
                headers: {'Content-Type': 'application/json'}
                }).then(function(response) {
                   return response.data;
               });
           }
           return promise;
       }

This is my controller code..

ProjectServices.getLoginAuthentication($scope.loginData).then(function(data) {
    if(data=="true"){

     $state.go('app.calendar',{username:$scope.loginData.userName}); 

 }
else{
      $scope.myvalue = true;
      $scope.loginData.password=''; 
        }
    });

can anybody please help me how to do this in simple javascript..?

Upvotes: 2

Views: 89

Answers (1)

svarog
svarog

Reputation: 9847

$http is just a wrapper for the XMLHttpRequest object that returns HTTP responses wrapped in $q promises. If you want to simulate that mechanism with plain JS, use the XMLHttpRequest object to send GET (or any other) requests and wrap the results with a Promise

http://mdn.beonex.com/en/DOM/XMLHttpRequest/Using_XMLHttpRequest.html

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

Upvotes: 1

Related Questions