【发布时间】:2017-07-08 08:51:24
【问题描述】:
我已经在https://angular.io/docs/ts/latest/tutorial/toh-pt6.html 上尝试了 http Angular 2 和 TypeScript 示例,它可以工作。 https://embed.plnkr.co/?show=preview
更新代码以使用外部 Web Api
private headers = new Headers({'Content-Type': 'application/json'});
// private heroesUrl = 'api/heroes'; // URL to web api
private heroesUrl = 'http://localhost/WebApi2/api/hero'; // URL to web api
constructor(private http: Http) { }
getHeroes(): Promise<Hero[]> {
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => response.json().data as Hero[])
.catch(this.handleError);
}
现在我想更新它以使用外部 Web Api2 并得到以下错误。
例外:未捕获(承诺中):响应状态:404 未找到 URL:http://localhost/WebApi2/api/hero" 发生错误 Object { _body: Object, status: 404, ok: false, statusText: "Not Found", headers: Object, type: null, url: "http://localhost/WebApi2/api/hero" }
我已经看过这个解决方案,但它对我不起作用。 Angular2 http get request results into 404
导入错误
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { JSONP_PROVIDERS } from '@angular/http';
Http/Http/node_modules/@angular/http/index"' 没有导出的成员 'JSONP_PROVIDERS'。
谁能指出我正确的方向,例如从 Angular2 调用 Web Api2?
【问题讨论】:
-
服务器上不存在该资源。所以,要么你使用了错误的 URL,要么是服务器问题,这与 Angular 无关。
-
您提供的 Plunker 链接不包含您的任何代码。将其更新为绝对链接
-
Angular 2 本身是否有 Promise 方法,或者您正在尝试使用 ES6 (TypeScript) Promise?
-
资源确实存在我已经在IIS中测试过了。
标签: angularjs typescript promise