如何从Google App Engine WebApp2中的Angular POST请求中获取数据? self.request.body返回一个字符串,不self.request.get(key)返回任何内容.
提交POST的Angular代码是:
$http.post("/mail", {request_name: 'Test Name', request_body: 'Test Body'});
Run Code Online (Sandbox Code Playgroud)
然后我的WebApp2处理程序中的这两行:
print "1: " + self.request.body
print "2: " + self.request.get('request_name')
Run Code Online (Sandbox Code Playgroud)
打印此:
1: {"request_name":"Test Name","request_body":"Test Body"}
2:
Run Code Online (Sandbox Code Playgroud)
从POST正文中获取数据的最佳方法是什么?或者我应该以不同方式发送请求?
情况就是这样:
我有一个简单的应用程序在Angular JS中制作,通过codeigniter中的API与服务器通信.
应用程序中有一个登录系统.当用户输入电子邮件和密码时,此数据将发送到服务器,如果电子邮件存在且密码匹配,则返回true.
我做了很多尝试,但没弄清楚我怎么能正确地做到这一点.
这是代码:
表格:
<form role="form" method="post" novalidate ng-submit="submitForm()">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" name="email" ng-model="user.name" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" name="password" ng-model="user.password" placeholder="Password">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
这是Angular js控制器:
$scope.authorized = false;
$scope.user = {};
$scope.submitForm = function()
{
console.log("posting data....");
$http({
method : 'POST',
url : 'http://127.0.0.1/api/main/login',
headers: {'Content-Type': 'application/json'},
data : JSON.stringify({email:$scope.user.email, password:$scope.user.password})
}).success(function(data) {
console.log(data);
$scope.authorized = data;
if ($scope.authorized) { $location.path("memberArea"); }; …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Angular $ http和以下代码进行跨源POST请求.
//I've tried setting and removing these http config options
$http.defaults.useXDomain = true;
delete $http.defaults.headers.common['X-Requested-With'];
$http.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded';
//Basic request, with some private headers removed
return $http({
method: 'POST',
//withCredentials:true,
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
params: params,
url: url
});
Run Code Online (Sandbox Code Playgroud)
预检OPTIONS请求获取200 OK,但后续POST收到400 Bad Request响应.查看Chrome调试窗口中的跟踪,我没有看到Content-Type: application/x-www-form-urlencoded; charset=UTF-8POST 的标题.我假设这就是服务器返回错误请求响应的原因.
我正在设置一些我从上面的代码中省略的其他自定义标题,并且它们正在被发送并显示正常.
我还要提一下,我可以使用Chrome的Advanced Rest Client应用程序发出此请求并收到正确的回复.(访问令牌)
我也试过做一个直接的XMLHttpRequest(),但我得到了同样的错误.
有关为什么我的Content-Type标头没有被设置的任何见解?
我有一个带有一些angularjs验证的Raw表单,
<form name = "sform" novalidate="true">
<table border="0">
<tr>
<td>
First name:
</td>
<td>
<input type="text" name="fname" ng-model="fname" placeholder="First Name" required="true" ng-pattern="/^[A-Za-z]*$/">
</td>
<td ng-show="sform.fname.$invalid && !sform.fname.$pristine" class="help-block">wrong name </td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<input type="text" ng-model="lname" placeholder="last Name" required="true" ng-pattern="/^[A-Za-z]*$/">
</td>
<td ng-show= "sform.lname.$invalid && !sform.lname.$pristine" class="help-block">wrong Last name </td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<input type="email" ng-model="email" placeholder="email" required="true">
</td>
<td ng-show="sform.email.$invalid && !sform.email.$pristine" class="help-block">wrong Email </td>
</tr>
<tr>
<td>
<button type="submit" ng-disabled="sform.$invalid" ng-click …Run Code Online (Sandbox Code Playgroud) $http({method: 'POST', url: 'http://localhost:5001/products', data: {token: $scope.product.token}}).success(
function () {
alert('success');
}
);
Run Code Online (Sandbox Code Playgroud)
在金字塔方面,request.POST显示NOVars:不是表单请求.不是HTML表单提交(Content-Type:application/json)
我使用檐口提供我的api(/产品),我认为这是金字塔的问题.
有没有人有办法解决吗?
我有一个布局页面,其中包含一个带有AntiForgeryToken的表单
using (Html.BeginForm(action, "Account", new { ReturnUrl = returnUrl }, FormMethod.Post, new { Id = "xcrf-form" }))
Run Code Online (Sandbox Code Playgroud)
这会生成一个隐藏字段
<input name="__RequestVerificationToken" type="hidden" value="p43bTJU6xjctQ-ETI7T0e_0lJX4UsbTz_IUjQjWddsu29Nx_UE5rcdOONiDhFcdjan88ngBe5_ZQbHTBieB2vVXgNJGNmfQpOm5ATPbifYE1">
Run Code Online (Sandbox Code Playgroud)
在我的角度视图中(在布局页面中加载div,我这样做
<form class="form" role="form" ng-submit="postReview()">
Run Code Online (Sandbox Code Playgroud)
我的postReview()代码如下
$scope.postReview = function () {
var token = $('[name=__RequestVerificationToken]').val();
var config = {
headers: {
"Content-Type": "multipart/form-data",
// the following when uncommented does not work either
//'RequestVerificationToken' : token
//"X-XSRF-TOKEN" : token
}
}
// tried the following, since my other MVC controllers (non-angular) send the token as part of form data, …Run Code Online (Sandbox Code Playgroud) 我正在使用AngularJS和PHPMailer构建一个应用程序.我需要将一个数组从我的Angular应用程序传递给PHPMailer,然后在PHPMailer中检索数组并将数据邮寄到客户端.
$scope.data = {};
var link = 'http://uvw.com/mail.php';
$http.post(link, {order:$scope.listItems}).then(function (res){
$scope.response = res.data;
console.log($scope.response);
});
Run Code Online (Sandbox Code Playgroud)
这里,$ scope.listItems是一个数组,包括客户名称,客户电子邮件,客户电话等主要字段以及每个头下的大量数据.所以归结为$ scope.listItems.customerName,$ scope.listItems.customerPhone等.我使用angular forEach非常容易地检索Angular中的数据.
我已成功将数据传递给PHPMailer,但我不知道如何在PHPMailer中检索数据并将其邮寄给它.
UPDATE
PHPMailer代码
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers:
{$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$postdata = file_get_contents("php://input");
if (isset($postdata)) {
$request = json_decode($postdata);
foreach ($order as $value) {
$body=$order;
}
require("PHPMailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->Username …Run Code Online (Sandbox Code Playgroud) 我正在通过AngularJS POST请求发送数据:
$http.post('/process', { 'uid': uid, 'action': action }).success(function(response) {
console.log(response);
});
Run Code Online (Sandbox Code Playgroud)
并尝试在Flask中获取发送的值
@app.route('/process', methods = ['POST'])
def process():
return json.dumps({ 'data': request.form.get('uid', 'EMPTY') })
Run Code Online (Sandbox Code Playgroud)
然后Flask返回{“ data”:“ EMPTY”}响应。request.form是空的。我尝试从中获取数据request.data,但是那里的格式很奇怪。
我正在学习Python和Flask,所以我想使用本机库来完成这项工作,而现在没有任何其他包。
我想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)
任何人都可以帮我传递数据吗?
angularjs ×9
php ×3
http ×2
http-headers ×2
post ×2
ajax ×1
asp.net-mvc ×1
codeigniter ×1
cornice ×1
cors ×1
flask ×1
ionic ×1
javascript ×1
json ×1
phpmailer ×1
pyramid ×1
python ×1
python-3.x ×1
webapp2 ×1