angularjs $ http post无法正常工作

问题描述:

我是angularjs中的新成员。我试图通过$ http post发布一些数据到服务器。我的代码可能会到达服务器,但数据不会传递。我使用golang作为后端。我在这里犯了什么错误?

completeCampaign.controller('campaignCtrl', ['$scope', '$http', function(scope, http) { 
    var Msg = "hai"; 
    http.post("/server_url",Msg).then(function(response) { 
     console.log(response); 
    }); 
}]); 

Go代码:

func (c *CarController) TestFunction() { 
    msg := c.GetString("Msg") 
    fmt.Println("Message is: ", msg) 
} 

输出:

Message is: 
+0

你得到的“消息”,而不是在服务器的身体可能 – Alessio

使用$符号:

$http.post("/server_url",Msg).then(function(response) { 
    console.log(response); 
}); 
+0

没有..现在它甚至没有到达服务器。我已经将'$ scope'定义为范围..'$ scope','$ http',函数(scope,http) –

“角$ HTTP POST接受JSON对象作为POST参数,而你只是发送一个字符串“(谢谢@ Kausik E vani)

此外,你有一个错字http,尝试更新你的代码。

completeCampaign.controller('campaignCtrl', ['$scope', '$http', function($scope, $http) { 
    var data = {msg: "hello"}; 

    $http.post("/server_url", data).then(function(response) { 
     console.log(response); 
    }); 
}]); 
+0

仍然不能正常工作 –

+0

您能确保您的Go代码正确吗? 。 http://stackoverflow.com/questions/15672556/handling-json-post-request-in-go – Alejandro

@AlejandroBáezArcila的答案当然是绝对正确的。对不起,作为一个迂腐,但它不完全是一个错字。而且OP最好能知道为什么他的POST不起作用。 Angular $ http post接受JSON对象作为POST参数,而你只是发送一个字符串。 就像@AlejandroBáezArcila建议的一样,将它发送为var data = {msg: "hai"};,并在服务器上只访问“msg”键。