Created
June 4, 2016 09:29
-
-
Save aquasmit/646a0a956577ac741ab32942a52b1a83 to your computer and use it in GitHub Desktop.
AngularJs $http post service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$http({ | |
method : 'POST', | |
url : 'http://localhost:3000/auth', | |
headers : {'Content-Type': 'application/x-www-form-urlencoded'}, //THIS IS VERY IMPORTANT. By default, AngularJs uses 'Content-Type' as 'application/json'. So we have modified it here. | |
transformRequest : function(obj){ //This IS IMPORTANT AS WELL. when we send the data with header 'Content-Type': 'application/x-www-form-urlencoded', then we have to make sure that we send the data in serialized format (var1=val1&var2=val2). So this function transforms our data in serialized format. | |
var str = []; | |
for(var p in obj) | |
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); | |
return str.join("&"); | |
}, | |
data : {username: $scope.username, password: $scope.password} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment