如何在Web应用程序中管理HTTP内容类型
- 论坛
- 如何在Web应用程序中管理HTTP内容类型
23 浏览
如何在Web应用程序中管理HTTP内容类型
我刚接触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); });
这个方法可行,但它同时使用了jQuery中的$.param
。为了摆脱对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会提供这个功能,但是怎么做呢?