【问题标题】:Post Request in Angular to Node.js is not sending在 Angular 中向 Node.js 发布请求未发送
【发布时间】:2019-03-07 17:05:45
【问题描述】:

我的目标很简单:我想发送一个帖子请求从 Angular 并通过 http 到我的 node.js

但是,没有收到请求。

这是我的 Angular 代码

const Data =  [{
'name' : this.userForm.controls.name.value,
'email' : this.userForm.controls.email.value,
'Object': this.userForm.controls.Object.value,
'message': this.userForm.controls.message.value
}];
console.log('ready to send');
this.http.post('http://localhost:8012/sendmail', Data).subscribe();

我只是在 Data 中格式化我的数据以发送它。这是我所有尝试的一部分。 网址很好,因为我使用 get 方法进行了测试并且它有效!


现在在 Node.js 中

const express = require('express');
const path = require("path");
const bodyParser = require('body-parser');
const mongoose = require('mongoose');

var db = mongoose.connect("mongodb://localhost:27017/test", 
function( err, response){
if(err){console.log(err);}
});

var app = express();
app.use(bodyParser());
app.use(bodyParser.json({limit:'10mb'}));
app.use(bodyParser.urlencoded({extended:true}));

app.use(function(req,res,next){
res.setHeader('Access-Control-Allow-
Origin','http://localhost:4200');
res.setHeader('Access-Control-Allow-
Methods','http://localhost:4200','POST');
res.setHeader('Access-Control-Allow- 
Credentials','http://localhost:4200', true);
next();
});

app.post("/sendmail",function (req, res, next){
console.log("wddddddf");
res.end('buck');
})


app.listen(8012, ()=>{
console.log('waf waf 8012 waf waf');
})

这里我只想接收数据和console.log以知道它正在工作。


我的问题是请求似乎无法到达我的节点。

有人可以帮我理解为什么吗?


编辑:它仍然不适用于订阅。我有这个错误:

【问题讨论】:

    标签: javascript node.js angular request rxjs


    【解决方案1】:

    经过数小时的搜索,我找到了解决方案: 我将此行添加为 setHeader:

    res.setHeader( 'Access-Control-Allow-Headers', 'Accept,Accept- 
    Language,Content-Language,Content-Type');
    

    【讨论】:

    • 你使用什么 Angular 版本?与 Http 服务相反,您不必使用 HttpClient 设置标头。
    • 6.2.2 并且,使用 Postman,请求有效。它只是有角度的
    • 肯定还有别的东西在起作用,还没用过 6.0,但自从我搬到 HttpClient 之后,我就再也不用设置标题了。
    【解决方案2】:

    您需要从 @angular/common/http 导入 HttpClient,如下所示。

    import {HttpClient, HttpHeaders} from '@angular/common/http';
    

    看看它是否有效。

    【讨论】:

    • 已经是了,我只是忘了我已经设置了这条路线。还是谢谢!
    【解决方案3】:

    您需要订阅post请求:

    this.http.post('http://localhost:8012/sendmail', Data)
        .subscribe();
    

    Angular Http 和从版本 4.3 开始的 HttpClient 使用 RxJS 流发送(使用 RxJS 术语发出)数据到后端。这些流是惰性求值的,这意味着除非订阅了它们,否则它们不会发出任何值。 因此,您需要像这样构建您的 http 调用:

        this.http.post(url, data)
            .map((response) => {    //optional
               //map over data - do some computation
            })
            .subscribe();
    

    通常前 4 行在服务中 - 包装在返回 Observable() 的函数中,.subscribe() 块在调用服务方法的组件中。

    【讨论】:

    • 感谢您的解释。但是,这似乎不是我的代码中唯一的错误,因为它仍然无法正常工作。我从 @angular/http 导入 HttpClient 并且不从 rxjs 导入任何东西会不会出现问题?
    【解决方案4】:

    您的请求未发送,因为您尚未订阅 post 请求

    this.http.post('http://localhost:8012/sendmail', Data)
                .subscribe((result) => console.log(result));
    

    【讨论】:

    • 谢谢,但这似乎不是我的代码中唯一的错误。
    猜你喜欢
    • 2020-10-30
    • 2019-04-08
    • 1970-01-01
    • 2018-09-06
    • 2020-09-17
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多