【发布时间】:2015-12-22 21:46:58
【问题描述】:
我正在尝试用$http.get() 替换我的$.ajax 呼叫。我尝试时收到 404 Not Found 错误。
这里是ajax调用:
// ToDo: See if there is an $http.get equivalent. That way the callback doesn't have
// to be wrapped in $scope.apply().
$.ajax({
url: '/PrestoWeb/api/ping/responses/',
type: 'POST',
data: JSON.stringify(latestPingRequest),
contentType: "application/json",
success: function (responses) {
// do stuff
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
这是 $http 调用:
var config = {
url: '/PrestoWeb/api/ping/responses/',
method: 'POST',
data: JSON.stringify(latestPingRequest),
contentType: "application/json"
};
$http.get(config)
.then(function (response) {
// do stuff
}, function (response) {
alert(response);
});
ajax 调用有效。 http调用没有。两个调用中的 URL、类型和数据完全相同。我错过了什么?
【问题讨论】:
-
你知道,
$http.get和method: post是矛盾的,对吧? -
您正在原始 $.ajax 调用中发帖。你要发 POST 还是 GET?如果发布,那么你应该使用 $http.post(config)
-
这些投票者太荒谬了。为什么投反对票?如果是因为 OP 不知道使用 $.post 而不是 $.get,那是因为他清楚地展示了他的代码以及他遇到的问题。对我来说似乎是个好问题
-
$http.post()和$http.get()是快捷方式。第一个参数应该是一个 URL。你甚至不需要配置对象,因为 Angular 会为你做这些事情:$http.post(url, data); -
$http.post(url, data, config)。此外,$http 模块会自动为您进行字符串化,因此您无需自己执行此操作。编辑是@SunilD。说,你可能只需要$http.post(url, { some: 'data' })
标签: javascript angularjs