当我将带有信息的表格发送到数据库(mongodb)时,我遇到了角度问题。这些是错误:
错误:错误{目标:XMLHttpRequest,isTrusted:true,lengthComputable:false,…}
标头:对象{normalizedNames:Map(0),lazyUpdate:null,标头:Map(0)}
消息:“对localhost:3700 / api / save-project的Http错误响应:0未知错误”
名称:“ HttpErrorResponse”
好:错误
状态:0
statusText:“未知错误”
url:“ localhost:3700 / api / save-project”
以及mi app.js和执行请求的组件的代码是这样的:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
//cargar archivos de rutas
var project_routes = require('./routes/project');
//middlewares
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
// Configurar cabeceras y cors
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Authorization, X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Request-Method');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
res.header('Allow', 'GET, POST, OPTIONS, PUT, DELETE');
next();
});
//rutas
app.use('/api', project_routes);
//exportar
module.exports = app;
角组件:
import { Component, OnInit } from '@angular/core';
import { Project } from '../../models/project';
import { projectService } from '../../services/project.service';
@Component({
selector: 'app-create',
templateUrl: './create.component.html',
styleUrls: ['./create.component.css'],
providers: [projectService]
})
export class CreateComponent implements OnInit {
public title: string;
public project: Project;
constructor( private _projectService: projectService)
{
this.title = 'crear projecto';
this.project = new Project('','','','',2019,'','');
}
ngOnInit() {
}
onSubmit(form){
console.log(this.project);
this._projectService.saveProject(this.project).subscribe(
response => {
console.log(response);
},
error => {
console.log(<any>error);
}
);
}
}
这是create.component使用的服务:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders} from '@angular/common/http';
import { Observable } from 'rxjs';
import { Project } from '../models/project';
import { global } from './global';
@Injectable()
export class projectService
{
public url: string;
constructor( private _httpClient: HttpClient)
{
this.url = global.url;
}
testService(){
return 'probando el servicio de angular';
}
saveProject(project: Project){
let params = JSON.stringify(project);
let headers = new HttpHeaders().set('Content-Type','application/json');
return this._httpClient.post(this.url + 'save-project', params, {headers:headers});
}
}
感谢您的时间<3