【问题标题】:http request work in postman but not in browserhttp请求在邮递员中有效,但在浏览器中无效
【发布时间】:2019-09-03 03:18:56
【问题描述】:

我能够发送帖子并从邮递员那里获取请求,但是当我实际从浏览器发送该请求时,它无法获取记录并且在控制台中显示错误“正文:{错误:“未找到集合'未定义'”} ”。

尝试了 Get 和 Post 请求,它们都在 POSTMAN 中提供数据作为响应,但在浏览器中它不起作用。显示错误“body: {error: "Collection 'undefined' not found"}”。 在不同地方的同一个项目中,我也使用内存数据库,我可以对其进行 /GETRequest 并接收数据作为响应。

homepage.ts:=============
public AllItem: AllItems[] ;
getAllItems(): void {
    console.log('AA');
    this.itemService.getAllItems() //(this.AllItems)
      .subscribe(AllItem => this.AllItem = AllItem  );
      console.log(this.AllItem);
      console.log('EE');
  }

item.Service.ts:===============
private itemsUrl = 'api/items';  // URL to web api
private allItemsUrl = 'http://*************.azurewebsites.net/items';
getAllItems(): Observable<AllItems[]>{ 
console.log('CC');  
    return this.http.get<AllItems[]>(this.allItemsUrl)
    .pipe(
      tap(_ => this.log('fetched heroes')),
      catchError(this.handleError<AllItems[]>('getHeroes', []))
    );
}

// this get request work properly and gives response data from in-memoery-db
getItems(): Observable<Item[]> {
 return this.http.get<Item[]>(this.itemsUrl)
 .pipe(
      tap(_ => this.log('fetched heroes')),
      catchError(this.handleError<Item[]>('getHeroes', []))
    );
}

in POSTMAN it gives data as
{
    "items": [
        {
            "category": "Drink",
            "item": "Coffee",
            "price": "5$"
        }]
}

in Browser console
core.js:15724 ERROR 
body: {…}, url: "http://**********.azurewebsites.net/items", headers: HttpHeaders, status: 404, statusText: "Not Found"}
body: {error: "Collection 'undefined' not found"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
status: 404
statusText: "Not Found"
url: "http://*************.azurewebsites.net/items"
__proto__: Object

【问题讨论】:

    标签: http browser get request postman


    【解决方案1】:

    得到了解决方案,实际上我在同一个项目的其他一些地方使用了 in-memory-web-api, Not found collection 错误提示你之前使用过 angular-in-memory-web-api。您需要从您的项目中删除与此相关的所有内容,以便您能够使用外部 api 和 db。

    “InMemoryWebApiModule.forRoot(InMemoryDataService)” Angular in-memory-web-api,它替换了 HttpClient 模块的 HttpBackend 所以在使用实际服务器和数据库之前需要先将其删除

    在此之后,我遇到了另一个问题,即对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。

    为此,我们需要在 Azure 的节点服务器中使用以下内容。 var cors = require('cors'); app.use(cors({origin: '*'}));

    【讨论】:

      猜你喜欢
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 2020-07-26
      • 1970-01-01
      • 2018-03-19
      • 2021-01-14
      • 1970-01-01
      相关资源
      最近更新 更多