【发布时间】:2019-06-24 13:26:16
【问题描述】:
我正在使用 Angular 6,而我以前从未使用过 Angular。我需要将数据发送到 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");
})
但我无法访问服务器。我收到错误消息“出现错误”。 谁能帮我解决这个问题?
【问题讨论】:
-
http.post('http://localhost:port/go'否则它不是有效的 URL -
应该是有效的,能给我们看看网络流量吗?
-
@trichetriche — 在我听说 WWW 之前就已经支持相对 URL。
-
@Quentin 这很酷,但 Angular 可能需要 HTTP url 而不是相对的。此外,使用
/go并不是真正的相对,它会尝试获取文件夹的根...不仅不是很清楚,而且非常随机 -
@trichetriche — Angular 及其调用的底层
XMLHttpRequest对象在网页托管的 JavaScript 环境中运行。相对 URL 是相对于该页面的 URL。
标签: html node.js angular angular6