Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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
如何从角度发布方法->;NodeJs/ExpressJs->;MySQL_Mysql_Node.js_Angular_Express - Fatal编程技术网

如何从角度发布方法->;NodeJs/ExpressJs->;MySQL

如何从角度发布方法->;NodeJs/ExpressJs->;MySQL,mysql,node.js,angular,express,Mysql,Node.js,Angular,Express,GET方法工作得很好,但是当我在angular项目上调用POST方法时,似乎没有调用nodeJs上的POST函数。我做错了什么 角度服务: getAllProducts(): Observable<Product[]> { return this.http.get<Product[]>('http://localhost:3000/api/content'); } insertProduct(product: Product): Observable<Produ

GET方法工作得很好,但是当我在angular项目上调用POST方法时,似乎没有调用nodeJs上的POST函数。我做错了什么

角度服务:

getAllProducts():  Observable<Product[]> {
return this.http.get<Product[]>('http://localhost:3000/api/content');
}

insertProduct(product: Product): Observable<Product> {
return this.http.post<Product>('http://localhost:3000/api/content', product);
}
也许您需要在post请求中添加“内容类型”标题,如:

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

insertProduct(product: Product): Observable<Product> {
  return this.http.post<Product>('http://localhost:3000/api/content', product, {
    headers: new HttpHeaders({ 'Content-Type': 'application/json' })
  });
}
从'@angular/common/http'导入{HttpClient,HttpHeaders};
insertProduct(产品:产品):可观察{
返回此.http.post('http://localhost:3000/api/content",产品,{
标题:新的HttpHeaders({'Content-Type':'application/json'})
});
}
有关指导,请参阅本文。
import { HttpClient, HttpHeaders } from '@angular/common/http';

insertProduct(product: Product): Observable<Product> {
  return this.http.post<Product>('http://localhost:3000/api/content', product, {
    headers: new HttpHeaders({ 'Content-Type': 'application/json' })
  });
}