我在laravel5.8中开发了自己的应用程序接口,并在Angular8中使用它。
当我在Postman中使用它时,我的应用程序接口工作得很好,但当我在angular8中使用它时,问题就来了。我从laravel api得到了空响应,如果我从浏览器点击了这个url,那么我就会得到json数据,但无法进入angular8。并且控制台中没有错误。
这是我的服务代码
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class DataService {
public url = 'http://archimate360.localhost/api/products';
constructor(private http:HttpClient) { }
getProducts(){
return this.http.get(this.url);
}
getProductDetails(id){
return this.http.get(this.url + 'products?id=' + id);
}
createProduct(data){
return this.http.post(this.url + 'products',data);
}
updateProduct(data){
return this.http.post(this.url + 'products' , data);
}
deleteProduct(id){
return this.http.get(this.url + 'products?id=' + id);
}
}发布于 2019-10-21 20:05:43
您是否创建了Cors中间件: App\Http\Middleware\Cors.php?如果没有,请尝试此1
如果您已经添加了CORS中间件,请在.ts文件中尝试此代码。如果您正在接收任何数据,它将在控制台上显示这些数据,如果您面临任何错误,它也将在控制台上显示这些数据,这将帮助我们识别错误
constructor(
private dataService: DataService
){ }
ngOnInit() {
this.dataService.getProducts().subscribe(
data => {
console.log(data)
},
error => {
console.log(error)
}
);
}发布于 2019-10-21 20:17:28
另一种解决方案是
getProducts(): any {
return this.http
.get(this.API_URL + '/product')
.map((res: Response) => {
return res;
})
.catch(this.handleError);
}https://stackoverflow.com/questions/58485119
复制相似问题