从Angular向服务器发送数据时,请求正文为空

时间:2018-05-27 19:01:07

标签: node.js angular express body-parser

我正在尝试从客户端(在Angular中)获取数据到nodejs服务器。

在Angular中我宣布了一项服务。

export class AddTaskService {

  constructor(private http: HttpClient) { }
  url = 'http://localhost:3000/tasks';
  posttasks(task) {
    return this.http.post<string>(this.url, JSON.stringify({ task: task }));
  }

}

在服务器中我添加了bodyparser并对其进行了配置

index.js

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

router.js

router
    .route("/tasks")
    .post(function (req, res) {
        var taskModel = new taskSchema();
        taskModel.task = req.body.task;
        console.log(req.body);
        taskModel.save(function (err) {
            if (err) {
                res.send(err);
            }
            res.json({
                message: "nouvProj created!"
            });
        });
    });

使用x-www-form-urlencoded工作的邮递员,但在II发送数据req.body时为Angular

编辑: 这是url网络详细信息: 的常规

Request URL: http://localhost:3000/tasks
Request Method: POST
Status Code: 200 OK
Remote Address: [::1]:3000
Referrer Policy: no-referrer-when-downgrade

响应标题

Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Methods: GET, POST, PUT ,DELETE
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 31
Content-Type: application/json; charset=utf-8
Date: Sun, 27 May 2018 19:24:57 GMT
ETag: W/"1f-NlZ/5EsK7Z/S3Ze5LQN4AonQQ90"
X-Powered-By: Express

请求标题

Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: fr-FR,fr;q=0.9,ar-TN;q=0.8,ar;q=0.7,en-US;q=0.6,en;q=0.5
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 14
Content-Type: text/plain
DNT: 1
Host: localhost:3000
Origin: http://localhost:4200
Pragma: no-cache
Referer: http://localhost:4200/
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36

请求有效负载

{task: "klj"}
task
:
"klj"

3 个答案:

答案 0 :(得分:2)

您可以将HTTP标头作为选项传递:

<div class="container">
  <img src="http:placehold.it/250x250">
  <div class="btn-container">
    <button>button 1 </button>
    <button>button 1 </button>
  </div>
</div>
<br/>
<br/>
<br/>

<h3>If u want space between buttons</h3>
<div class="container">
  <img src="http:placehold.it/250x250">
  <div class="btn-container">
    <div class="btn-holder b1">
      <button>button 1 </button>
    </div>
    <div class="btn-holder b2">
      <button>button 1 </button>
    </div>
  </div>
</div>

答案 1 :(得分:0)

正如@约书亚(Joshua)所建议的,但是您只需要稍作改动

const httpOptions = {
    headers: new HttpHeaders({
        'Content-Type': 'application/x-www-form-urlencoded',
    })
}
return this.http.post<string>(this.url, {task, httpOptions});

,并且在服务器API中,您必须获取

之类的数据
const data = req.body.task  

答案 2 :(得分:0)

createTask(task) {
  const httpOptions= {headers: new HttpHeaders({'Content-Type':'application/json})};
  const json = JSON.stringify(task);
  
  this.http.post(this.url, json, this.httpOptions);
}