我正在使用RestAPI创建一个CRUD应用程序。我知道这个问题在堆栈溢出中已经被问过很多次了,但是我已经看到了很多关于这个问题的答案,但并不能解决我的错误。 实际上,我正在使用RestAPI添加产品,为此,我使用了显而易见的get()方法。 HTTPResponse始终在Uri localhost:4200上返回状态404。 请帮助我解决这个问题,从昨晚开始我就陷入了这个问题。
Product.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class ProductsService {
uri = 'http://localhost:4200/products';
constructor(private http: HttpClient) { }
addProduct(ProductName, ProductDescription, ProductPrice) {
console.log(ProductName, ProductDescription, ProductPrice);
const obj = {
ProductName,
ProductDescription,
ProductPrice
};
this.http.post(`${this.uri}/add`, obj)
.subscribe(res => console.log('Done'));
}
getProducts() {
return this
.http
.get(`${this.uri}`);
}
editProduct(id) {
return this
.http
.get(`${this.uri}/edit/${id}`);
}
updateProduct(ProductName, ProductDescription, ProductPrice, id) {
const obj = {
ProductName,
ProductDescription,
ProductPrice
};
this
.http
.post(`${this.uri}/update/${id}`, obj)
.subscribe(res => console.log('Update Complete'));
}
deleteProduct(id) {
return this
.http
.get(`${this.uri}/delete/${id}`);
}
}
App-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ProductAddComponent } from './product-add/product-add.component';
import { ProductEditComponent } from './product-edit/product-edit.component';
import { ProductGetComponent } from './product-get/product-get.component';
const routes: Routes = [
{
path: 'product/create',
component: ProductAddComponent
},
{
path: 'edit/:id',
component: ProductEditComponent
},
{
path: 'products',
component: ProductGetComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }