在下面的代码中,AngularJS $http方法调用URL,并将xsrf对象作为"请求有效负载"提交(如Chrome调试器网络选项卡中所述).jQuery $.ajax方法执行相同的调用,但将xsrf提交为"Form Data".
如何让AngularJS将xsrf作为表单数据而不是请求有效负载提交?
var url = 'http://somewhere.com/';
var xsrf = {fkey: 'xsrf key'};
$http({
method: 'POST',
url: url,
data: xsrf
}).success(function () {});
$.ajax({
type: 'POST',
url: url,
data: xsrf,
dataType: 'json',
success: function() {}
});
Run Code Online (Sandbox Code Playgroud) 我是AngularJS的新手,一开始,我想只使用AngularJS开发一个新的应用程序.
我正在尝试使用$http我的Angular App 对服务器端进行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);
});
Run Code Online (Sandbox Code Playgroud)
这是有效的,但它也使用jQuery $.param.为了消除对jQuery的依赖,我尝试了:
data: {username: $scope.userName, password: $scope.password}
Run Code Online (Sandbox Code Playgroud)
但这似乎失败了.然后我尝试了params:
params: {username: $scope.userName, password: $scope.password}
Run Code Online (Sandbox Code Playgroud)
但这似乎也失败了.然后我尝试了JSON.stringify:
data: JSON.stringify({username: $scope.userName, password: $scope.password})
Run Code Online (Sandbox Code Playgroud)
我找到了这些可能的答案,但没有成功.难道我做错了什么?我相信,AngularJS会提供这个功能,但是如何?