在下面的代码中,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会提供这个功能,但是如何?
我有一些旧的代码通过jQuery的post方法发出一个AJAX POST请求,看起来像这样:
$.post("/foo/bar", requestData,
function(responseData)
{
//do stuff with response
}
Run Code Online (Sandbox Code Playgroud)
requestData 只是一个具有一些基本字符串属性的javascript对象.
我正在将我们的东西移动到使用Angular,我想用$ http.post替换这个调用.我想出了以下内容:
$http.post("/foo/bar", requestData).success(
function(responseData) {
//do stuff with response
}
});
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我收到了来自服务器的500错误响应.使用Firebug,我发现这样就发送了请求体:
{"param1":"value1","param2":"value2","param3":"value3"}
Run Code Online (Sandbox Code Playgroud)
成功的jQuery $.post发送身体像这样:
param1=value1¶m2=value2¶m3=value3
Run Code Online (Sandbox Code Playgroud)
我遇到的端点是期望请求参数而不是JSON.所以,我的问题是,无论如何要告诉$http.post发送javascript对象作为请求参数而不是JSON?是的,我知道我可以自己从对象构造字符串,但我想知道Angular是否提供了开箱即用的东西.
我有一个带标签的表格 ng-submit="login()
该函数在javascript中被称为fine.
function LoginForm($scope, $http)
{
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
$scope.email = "fsdg@sdf.com";
$scope.password = "1234";
$scope.login = function()
{
data = {
'email' : $scope.email,
'password' : $scope.password
};
$http.post('resources/curl.php', data)
.success(function(data, status, headers, config)
{
console.log(status + ' - ' + data);
})
.error(function(data, status, headers, config)
{
console.log('error');
});
}
}
Run Code Online (Sandbox Code Playgroud)
我从PHP文件中得到200 OK响应,但是,返回的数据是这样email并且password未定义.这是我的所有PHP
<?php
$email = $_POST['email'];
$pass = $_POST['password'];
echo $email;
?>
Run Code Online (Sandbox Code Playgroud)
知道为什么我得到未定义的POST值吗?
编辑
我想指出,因为这似乎是一个受欢迎的问题(但它已经过时了),.success并且.error已经被弃用了,你应该使用 …
我刚刚更新了Angular + Ionic上的新版本以及处理远程请求停止工作的方法,并始终返回404响应.
请求如下:
Request Method:POST
Status Code:404 Not Found (from cache)
Request Headersview source
Accept:application/json, text/plain, */*
Content-Type:text/plain
Origin:file://
User-Agent:Mozilla/5.0 (Linux; Android 4.4.2; Lenovo Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36
Request Payloadview source
{,…}
Run Code Online (Sandbox Code Playgroud)
处理远程请求的方法的代码如下:
// set transfer credentials
$http({
method : 'POST',
url : $scope.remoteUrl,
data: {img_base64: "/9j/4AAQSkZ"},
headers: 'application/json',
timeout: 10000
// success response
}).success(function(data, status, headers, config) {
//SUCESSS
} else {
//PROCESSING ERROR
}
// error response
}).error(function(data, status, headers, config) { …Run Code Online (Sandbox Code Playgroud) 以下$http request执行成功,但另一端的PHP脚本在$_POST收到'test'和'testval'时会收到一个空数组.有任何想法吗?
$http({
url: 'backend.php',
method: "POST",
data: {'test': 'testval'},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data, status, headers, config) {
console.log(data);
}).error(function (data, status, headers, config) {});
Run Code Online (Sandbox Code Playgroud) 当我使用$http.get我的代码工作,但如果我使用$ http.post,我永远不会得到请求.php文件的参数.
这是服务功能:
TestPanel.service('MySampleService', function ($http, $q) {
this.getAllPosts = function () {
var def = $q.defer();
$http.post('/data/AJAXRequest.php', 'mydata=1&abcd=2').success(function (data) {
if (data == null)
def.reject('ERROR: DATA IS NULL');
else if (data.HasError)
def.reject('ERROR: ' + data.Message);
else
def.resolve(data);
}).error(function () {
def.reject('ERROR: Sorry, unable to complete your request.');
});
return def.promise;
}
});
Run Code Online (Sandbox Code Playgroud)
和控制器功能:
TestController.controller('PostCtrl', ['$scope', '$http', 'MySampleService',
function ($scope, $http, MySampleService) {
function FetchPostsList() {
MySampleService.getAllPosts().then(function (data) {
$scope.lstPosts = data.ResponseData;
$scope.totalRecords = data.totalRecords;
console.info('DATA=' + $scope.lstPosts); …Run Code Online (Sandbox Code Playgroud) 我已经用jQuery编写针对ASP.NET Web API的代码了一段时间,我在Angular中开始了一些新的东西(针对相同的Web API后端编写.)
我正在发布一个方法,该方法将返回系统中实体的一些搜索结果.它看起来像这样:
public IEnumerable<dynamic> Post(string entity, FormDataCollection parameters)
{
// entity is passed on the route.
// parameters contains all the stuff I'm trying to get here.
}
Run Code Online (Sandbox Code Playgroud)
如果我使用jQuery.post调用该方法:
$.post('api/search/child', {where : 'ParentID = 1'}, function(d){ foo = d });
Run Code Online (Sandbox Code Playgroud)
它运作正常,并返回我期望的.
我在我的角度应用程序中提供了一个类似的调用服务:
$http.post('api/search/child', { where: 'parentID = ' + parent.ID })
.success(function (data, status, headers, config) {
// etc.
})
Run Code Online (Sandbox Code Playgroud)
但是当它在服务器上点击我的"Post"方法时,"paramters"为空.
经过一些谷歌搜索后,我尝试添加一个内容类型的标题,以确保它作为JSON传递,并尝试JSON.stringify-ing和$ .param() - "数据"参数,但没有做任何事情(和从我所读到的那些不应该是必要的.)我在这里做错了什么?谢谢你的帮助!
更新:这是来自(工作)jQuery示例的原始请求:
POST http://localhost:51383/api/search/child HTTP/1.1
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: …Run Code Online (Sandbox Code Playgroud) 我只是想POST用JS向服务器发送请求.但服务器有空$_POST数组.我可以使用HTTP_RAW_POST_DATA,但它将在PHP 5.6中弃用.我可以在我的$_POST阵列中发布数据吗?
环境:Chrome,apache2,PHP,AngularJS(我正在使用$http.post功能).
调试图像(抱歉没有直接附加图像 - 我没有10个声誉)
我想POST从离子应用程序向远程php文件发送请求,以将base64编码数据保存在数据库中.当我传递POST数据时,我可以看到post参数被发送,但是当我从php文件打印响应时,它是空白的.
我试过这段代码:
controller.js
$http.post("someurl", {data: {"data333": 'peter'}});
Run Code Online (Sandbox Code Playgroud)
当我打印$_POST或$_REQUEST从php,它是一个空白数组,但从我的控制台,我可以看到参数与json一起传递{data: {"data333": 'peter'}}.
我已在客户端和服务器中允许跨域.
我也试过标准的ajax方式:
$http.defaults.headers.post["Content-Type"] = 'application/x-www-form-urlencoded; charset=UTF-8';
$http({
method : 'POST',
url : 'someurl/',
headers: {
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
},
data : {"key": "value"}
})
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我传递数据吗?