试图通过Jekyll中的角度js发布。有可能吗?
app.controller('myCtrl', function($scope) {
$scope.formData = {"name":"myname", "mail":"mymail@mail.hu", "message":"msg"};
$scope.sendContactSubmit = function() {
var name_val = $scope.formData.name;
var email_val = $scope.formData.mail;
var message_val = $scope.formData.message;
var jsondata = "{\"contact\": { \"name\": \"" + name_val + "\", \"email\": \"" + email_val + "\", \"message\": \"" + message_val + "\" }}";
alert("click2 " + jsondata);
$http({
url: "xxx.com/api/contact",
type: "POST",
data: jsondata,
dataType: 'json',
headers : { 'Content-Type': 'application/json' }
}).success(function (data) {
alert(data)
});
}
});
这是我的代码。直到"点击2"工作,然后什么也没发生。我可以在jekyll和github页面中使用angular和http post吗?
答案 0 :(得分:1)
将$http
注入控制器
app.controller('myCtrl', function($scope,$http) {
还可以使用then
来捕获响应。新版本中不推荐使用success
$http({
url: "xxx.com/api/contact",
method: "POST",
data: jsondata,
dataType: 'json',
headers: {
'Content-Type': 'application/json'
}
}).then(function(response) {
alert(response.data)
});