【发布时间】:2016-11-21 21:36:16
【问题描述】:
我是 AngularJS 的新手,一开始我想开发一个只使用 AngularJS 的新应用程序。
我正在尝试使用 Angular 应用程序中的 $http 对服务器端进行 AJAX 调用。
为了发送参数,我尝试了以下方法:
$http({
method: "post",
url: URL,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({username: $scope.userName, password: $scope.password})
}).success(function(result){
console.log(result);
});
这是可行的,但它也在 $.param 使用 jQuery。为了消除对 jQuery 的依赖,我尝试了:
data: {username: $scope.userName, password: $scope.password}
但这似乎失败了。然后我尝试了params:
params: {username: $scope.userName, password: $scope.password}
但这似乎也失败了。然后我尝试了JSON.stringify:
data: JSON.stringify({username: $scope.userName, password: $scope.password})
我为我的任务找到了这些可能的答案,但没有成功。难道我做错了什么?我敢肯定,AngularJS 会提供这个功能,但是如何提供呢?
【问题讨论】:
-
我不知道什么是实际问题,但你试过这个
$http({method: 'post', url: URL, data: {username: $scope.userName, password: $scope.password}}); -
你的第一个方法应该可以工作,
$scope.userName定义了吗?你为什么不试试data: data? -
@KevinB: 抱歉.. 我做了正确的编辑。
-
@mritunjay:对不起..我已经编辑了..我也在尝试。
-
@Veer 是否有效,还是您仍有问题?
标签: javascript jquery angularjs ajax angularjs-http