I am new in angularjs. I was trying to post some data to the server through $http post. My code could reach the server, but data is not passed. I am using golang for back-end. What are the mistakes I made here?
completeCampaign.controller('campaignCtrl', ['$scope', '$http', function(scope, http) {
var Msg = "hai";
http.post("/server_url",Msg).then(function(response) {
console.log(response);
});
}]);
go code:
func (c *CarController) TestFunction() {
msg := c.GetString("Msg")
fmt.Println("Message is: ", msg)
}
output:
Message is:
Use $ sign:
$http.post("/server_url",Msg).then(function(response) {
console.log(response);
});
"Angular $http post accepts JSON objects as POST parameters, whereas you are just sending a string" (thanks @Kaushik Evani)
also you have a typo in http, try to update your code to this.
completeCampaign.controller('campaignCtrl', ['$scope', '$http', function($scope, $http) {
var data = {msg: "hello"};
$http.post("/server_url", data).then(function(response) {
console.log(response);
});
}]);
@Alejandro Báez Arcila's answer is of course absolutely right. Sorry for being a pedantic, but it's not exactly a typo. And also it would better for the OP to know why his POST is not working. Angular $http post accepts JSON objects as POST parameters, whereas you are just sending a string. So like @Alejandro Báez Arcila has suggested, send it like var data = {msg: "hai"};
and just access "msg" key on your server.