Sunday, 30 April 2017

How to use rest api in client side?

Hello today I have simple code Nodejs express that work as rest api. It work when i use postman to send request as get method like code below. But i try use it in client. it doesn't work . So can you help find mistake,please?

// simple rest api 
router.get('/getuser', function(req, res, next) {
   var connection = getcon();

    res.setHeader('Content-Type', 'application/json');
    connection.query('SELECT username,password from tbuser', function (error, results, fields) {
        if (error) throw error;
        console.log('Object : ', JSON.stringify(results));
        res.send(JSON.stringify(results));
    });
    connection.end();
});

// client code 
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
  $http.get("http://localhost:3000/users/getuser")
  .then(function(response) {
      $scope.myWelcome = response.data;

  });
});
</script>



via chen rina

No comments:

Post a Comment