[英]How to pass data from angular 6 to Nodejs?
我使用的是角度6,而我以前從未使用過角度。 我需要將數據發送到nodejs服務器。 我在角度函數中使用以下代碼
import { Component, OnInit } from '@angular/core';
import { HttpClient,HttpHeaders } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
fun_add(Id, cat) {
return this.http.post('/go', data,
httpOptions).subscribe(result => {
console.log(result);
}, error => console.log('There was an error: '));
}
在我的nodejs中
app.post('/go', function (req, res) {
console.log("hi");
})
但是我不能去服務器。 我收到錯誤消息味精'有一個錯誤'。 有人可以幫我嗎?
您永遠不會發送響應(例如,使用res.send()
),因此服務器獲取請求並將其放置在那里。
同時,客戶端等待,然后等待,最終超時並拋出錯誤,因為服務器從未響應。
let api_url = 'http:localhost:portNumber'
fun_add(Id, cat) {
return this.http.post(api_url+'/go', data,
httpOptions).subscribe(result => {
console.log(result);
}, error => console.log('There was an error: '));
}
在組件中添加此代碼
constructor(private http: HttpClient);
工作正常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.