Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Angular和NodeJ访问控制允许标头错误_Javascript_Node.js_Angular_Express_Http - Fatal编程技术网

Javascript 使用Angular和NodeJ访问控制允许标头错误

Javascript 使用Angular和NodeJ访问控制允许标头错误,javascript,node.js,angular,express,http,Javascript,Node.js,Angular,Express,Http,我试图使用节点服务器向我的api发送帖子,我使用http类生成标题,但即使设置了标题(已纠正),我仍然会收到“访问控制允许标题”我做错了什么 这是我的app.js(api): 这是我的发帖请求: { const post : Huesped = {id:id,adultos:adultos,ninos:ninos,nombre:nombre, estatus:estatus,llegada:llegada,salida:salida,noches:noches,porPagar:porP

我试图使用节点服务器向我的api发送帖子,我使用http类生成标题,但即使设置了标题(已纠正),我仍然会收到“访问控制允许标题”我做错了什么

这是我的app.js(api):

这是我的发帖请求:

 {
   const post : Huesped = {id:id,adultos:adultos,ninos:ninos,nombre:nombre, estatus:estatus,llegada:llegada,salida:salida,noches:noches,porPagar:porPagar,pendiente:pendiente,origen:origen,habitacion:habitacion, telefono:telefono,email:email,motivo:motivo}
   this.http
     .post<{ message: string }>("http://localhost:3000/api/reportes", post)
     .subscribe(responseData => {
       console.log(responseData.message);
       this.huesped.push(post);
       this.postUpdated.next([...this.huesped]);
     });
 }  

{
const post:Huesped={id:id,adultos:adultos,ninos:ninos,nombre:nombre,estatus:estatus,llegada:llegada,salida:salida,noches:noches,porPagar:porPagar,pendiente:pendiente,origen:origen,habitacion:habitacion,telefono:telefono,email:email,motivo:motivo}
这是http
.post(“http://localhost:3000/api/reportes“,邮政)
.订阅(响应数据=>{
console.log(responseData.message);
这个.huesped.push(post);
this.postapded.next([…this.huesped]);
});
}  
每次我发出post请求时,chrome控制台都会向我显示CORS错误

“访问位于的XMLHttpRequest”http://localhost:3000/api/reportes“起源”http://localhost:4200'已被CORS策略阻止:飞行前响应中的访问控制允许标头不允许请求标头字段内容类型。“


我不知道我做错了什么

CORS由服务器控制。如果您使用的是express,请尝试其中一些。这在
选项上失败。你处理得对吗?
 {
   const post : Huesped = {id:id,adultos:adultos,ninos:ninos,nombre:nombre, estatus:estatus,llegada:llegada,salida:salida,noches:noches,porPagar:porPagar,pendiente:pendiente,origen:origen,habitacion:habitacion, telefono:telefono,email:email,motivo:motivo}
   this.http
     .post<{ message: string }>("http://localhost:3000/api/reportes", post)
     .subscribe(responseData => {
       console.log(responseData.message);
       this.huesped.push(post);
       this.postUpdated.next([...this.huesped]);
     });
 }