angular.js $ http.post不工作 - 没有错误 [英] angular.js $http.post not working - no error
问题描述
我试图通过提交HTTP $一个新的注释。你可能已经从标题猜对了:它不工作。我尝试了岸边版和长版,都失败。无控制台错误。
I'm trying to submit a new comment using $http. You might have guessed it from the title: it's not working. I tried the shore version and the long version, both fail. No console error.
这是我的code:
$scope.comment = {};
$scope.comment["comment"] = message; // message defined somewhere else
$http.post('/api/items/'+$scope.item.id+'/comments', $scope.comment)
.success(function(data, status, headers, config) {
// this isn't happening:
console.debug("saved comment", $scope.comment);
})
.error(function(data, status, headers, config) {
// this isn't happening:
console.debug("saved comment", $scope.comment);
})
}
任何人有关于如何使这项工作任何想法?谢谢!
Anyone got any idea on how to make this work? Thanks!
更新:
我做的是现在jQuery的Ajax调用,这是工作的罚款。它会是不错得到它具有角,虽然工作。这是我的JQuery $ C $但是C:
I'm doing it as a Jquery ajax call now, which is working fine. It'd be nice to get it to work with angular though. This is my JQuery code however:
var request = $.ajax({
url: '/api/items/'+$scope.item.id+'/comments',
type: "POST",
data: JSON.stringify($scope.comment),
contentType: 'application/json; charset=utf-8',
dataType: "json"
});
request.done(function(msg) {
console.debug("saved comment", $scope.comment);
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
如果任何人有任何想法如何angularify这个请告诉我,将是很好做的正确的方式....
If anyone has any ideas how to angularify this please tell me, would be nice to do it the proper way....
推荐答案
你有没有提出请求受审专门设置的Content-Type?
Have you tried specifically setting the Content-Type before making the request?
我有同样的问题和设置Content-Type的修复它。
I had the same issue and setting Content-Type fixes it.
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
这篇关于angular.js $ http.post不工作 - 没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!