https://codeforgeek.com/2014/07/angular-post-request-php/
您好我按照上面的链接从角js发送请求到节点js。当我提供
时,我收到了以下格式发布的数据console.log(req.body);
{ '{"email":"test@test.com","pass":"password"}': '' }
当我尝试获得如下所示的值时,它表示未定义。
var email = req.body.email;
console.log(email);
我无法获得电子邮件的价值并通过。谢谢
答案 0 :(得分:1)
将客户端标题代码更改为标题:{' Content-Type':' application / json' }
答案 1 :(得分:0)
您的Angular代码正在发送JSON数据,但您的Express应用程序正在将其解析为URL编码数据。
你的Express应用程序可能有这样的东西:
var bodyParser = require('body-parser');
...
app.use(bodyParser.urlencoded());
最后一行应该是:
app.use(bodyParser.json());
答案 2 :(得分:0)
你没有很好地解释这个问题,请下次尝试发布你的代码的更大部分,这样我们就能理解你想要做什么/想说什么。
要回答您的问题,我将复制/粘贴我的代码的一部分,使您能够从前端应用程序(angularJS)接收发送请求到您的后端应用程序(NodeJS),以及另一个使您能够执行逆转的功能从nodeJS发送一个post请求到另一个应用程序(可能会使用它):
1)收到angularJS发送的请求或nodeJS app中的任何内容
<div class="modal-body">
<form class="form-horizontal" enctype = "multipart/form-data">
<div class="form-group">
<strong>
<div>
<h3 class="modal-title text-center" id="myModalLabel">
<strong>Republic of the Philippines<br>Province of Negros Occidental</strong></h3>
<h4 class ="text-center"><em>City of Victorias</em></h4>
<h4 class="text-center">
<strong>OFFICE OF THE MARKET SUPERVISOR</strong>
</h4>
<br>
<div class="form-group">
<input type = "hidden" class="form-control" name= "id" placeholder="First Name" value = "<?php echo $value['rate_id'];?>">
<label class="col-sm-2 control-label">Name </label>: <?php echo $value['tenant_fname']." ".$value['tenant_mname']." ".$value['tenant_lname'];?>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Location </label>: Stall No. <?php echo $value['stall_number']." (".$value['stall_floor'];?>)
</div>
<div class="form-group">
<label class="col-sm-2 control-label">SUBJECT </label> :
FOR PAYMENT OF ELECTRICAL CONSUMPTION FOR THE PERIOD FROM
<input class = "datepick custom-input1"></input>
,TO
<input class = "datepick custom-input1"></input>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">METER NO. </label>: <input type="text" name="" class = "custom-input"></input>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">ELECTRIC RATE </label>: <input class = "custom-input" value="<?php echo $value['elec_rate']?> per kWh" id= "rate" readonly ></input>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">PRESENT </label>: <input class = "custom-input" id="current" name="current"></input>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">PREVIOUS </label>: <input class = "custom-input" value ="<?php echo $value['charges']?> kWh" id = "previous" readonly> </input>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">TOTAL CONSUMPTION</label>:
<input type="text" class = "custom-input" type="" id="result1" readonly>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">AMOUNT </label>:
<input type="text" class = "custom-input" type="" id="result" readonly>
</div><br>
</strong>
</div>
</div>
2)反向发送从nodeJS应用程序到另一个应用程序的POST请求
//Import the necessary libraries/declare the necessary objects
var express = require("express");
var myParser = require("body-parser");
var app = express();
// we will need the following imports for the inverse operation
var https = require('https')
var querystring = require('querystring')
// we need these variables for the post request:
var Vorname ;
var Name ;
var e_mail ;
var Strasse ;
app.use(myParser.urlencoded({extended : true}));
// the post request is send from http://localhost:8080/yourpath
app.post("/yourpath", function(request, response ) {
// test the post request
if (!request.body) return res.sendStatus(400);
// fill the variables with the user data
Vorname =request.body.Vorname;
Name =request.body.Name;
e_mail =request.body.e_mail;
Strasse =request.body.Strasse;
response.status(200).send(request.body.title);
});
最后,我希望这个答案可以帮助那些正在研究如何在节点JS中获取发布请求以及如何从nodeJS应用程序发送Post请求的人。
有关如何接收帖子请求的更多详细信息,请阅读body-parser库的npm文档:npm official website documentation
我希望你喜欢这个和Vielaspaß(享受德语乐趣)。