-1

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:
Arjun Ajith
  • 1,850
  • 5
  • 21
  • 46

3 Answers3

2

Use $ sign:

$http.post("/server_url",Msg).then(function(response) {
    console.log(response);
});
vaqifrv
  • 754
  • 1
  • 6
  • 20
  • Nope.. Now it is not even reaching the server. I already defined '$scope' to scope.. '$scope', '$http', function(scope, http) – Arjun Ajith Jul 26 '16 at 13:32
1

"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
  • 270
  • 2
  • 12
  • can you make sure your Go code is correct?, i mean handling post request in go. http://stackoverflow.com/questions/15672556/handling-json-post-request-in-go – Alejandro Jul 26 '16 at 13:56
1

@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.

Kaushik Evani
  • 1,154
  • 9
  • 17