【发布时间】: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