首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Angular8中使用laravel5.8API的问题

在Angular8中使用laravel5.8API的问题
EN

Stack Overflow用户
提问于 2019-10-21 19:15:13
回答 2查看 37关注 0票数 0

我在laravel5.8中开发了自己的应用程序接口,并在Angular8中使用它。

当我在Postman中使用它时,我的应用程序接口工作得很好,但当我在angular8中使用它时,问题就来了。我从laravel api得到了空响应,如果我从浏览器点击了这个url,那么我就会得到json数据,但无法进入angular8。并且控制台中没有错误。

这是我的服务代码

代码语言:javascript
复制
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);
  }
}
EN

回答 2

Stack Overflow用户

发布于 2019-10-21 20:05:43

您是否创建了Cors中间件: App\Http\Middleware\Cors.php?如果没有,请尝试此1

如果您已经添加了CORS中间件,请在.ts文件中尝试此代码。如果您正在接收任何数据,它将在控制台上显示这些数据,如果您面临任何错误,它也将在控制台上显示这些数据,这将帮助我们识别错误

代码语言:javascript
复制
constructor(
private dataService: DataService
){ }


ngOnInit() {
    this.dataService.getProducts().subscribe(
      data => {
       console.log(data)
      },
      error => {
      console.log(error)
      }
    );
  }
票数 0
EN

Stack Overflow用户

发布于 2019-10-21 20:17:28

另一种解决方案是

代码语言:javascript
复制
getProducts(): any {
    return this.http
        .get(this.API_URL + '/product')
        .map((res: Response) => {
            return res;
        })
        .catch(this.handleError);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58485119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档