1

Need to convert below curl command into angular http request. command looks similar to below command.

" curl -X POST --data 'username=myusername&password=mypassword' -i https://myurl.com/j_spring_security_check "

In https what I suppose to use, GET or POST. Can any one help in this.

Habib
  • 37
  • 8

1 Answers1

0

It could be a service that has a postMethod method that takes two arguments username and password just like this :

angular.factory('myService', ['$http', function($http){
    function postMethod(username, password){
        var request = {
            method:'POST',
            url: 'https://myurl.com/j_spring_security_check ',
            data: {
                username:username,
                password:password
            }
        }
        return $http(request);
    }

    return {
        postMethod:postMethod
    }
]);

Then you can use your service as following :

angular.module('myapp').controller('MainCtrl', ['myService', function(myService){
    myService.postMethod('foo', 'bar').then(function(response){
        // Do a UI change ?
    });
}]);
Zakaria
  • 14,892
  • 22
  • 84
  • 125
  • Thanks for response dude. Tried in similar way. I got below error... "Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost:9292' is therefore not allowed access". How can I resolve that error? – Habib Oct 01 '16 at 19:41
  • CORS issue ... if you are on chrome, the simplest way is to install the following extension to allow multi origin requests : https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en – Zakaria Oct 02 '16 at 09:18
  • Issue was fixed in my machine. Thanks a lot Zakaria. But I have small doubt here. I can't install this chrome extension in users machines. then how to solve this issue. – Habib Oct 02 '16 at 16:55
  • The most common way is to restrict origins on the server side. For more info: http://enable-cors.org/server.html – Zakaria Oct 02 '16 at 17:12