本文介绍了Angular2 http获取请求结果为404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在

此处:

private heroesUrl = 'http://www.mocky.io/v2/575ed63f1100004f1b2992f4';  // URL to web api

  constructor(private http: Http) { }

  getHeroes(): Promise<Hero[]> {
    this.http.get(this.heroesUrl)
               .toPromise()
               .then(response => response.json().data)
               .catch(this.handleError);
  }

我还尝试了我的本地服务器网址,例如 http :// localhost:5000 / api

I also tried my local server url such as http://localhost:5000/api

但没有任何效果,控制台显示

VM2510 hero.service.ts!transpiled:66 An error occurred Response {_body: Object, status: 404, ok: false, statusText: "Not Found", headers: Headers…}

完成例外

EXCEPTION:错误:未捕获(在承诺中):[object Object]
platform-b​​rowser.umd.js:962 EXCEPTION:错误:未捕获(在promise):[object Object] BrowserDomAdapter.logError @ platform-b​​rowser.umd.js:962BrowserDomAdapter.logGroup @ platform-b​​rowser.umd.js:972ExceptionHandler.call @ core.umd.js:3696(匿名函数)@core.umd .js:8951schedulerFn @ core.umd.js:6007SafeSubscriber .__ tryOrUnsub @ Subscriber.ts:240SafeSubscriber.next @ Subscriber.ts:192Subscriber._next @ Subscriber.ts:133Subscriber.next @ Subscriber.ts:93Subject._finalNe xt @ Subject.ts:154Subject._next @ Subject.ts:144Subject.next @ Subject.ts:90EventEmitter.emit @core.umd.js:5996onError @ core.umd.js:6227onHandleError @ core.umd.js:6096ZoneDelegate。 handleError @ [email protected]?main = browser:327Zone.runGuarded @ [email protected]?main = browser:233_loop_1 @ [email protected]?main = browser:487drainMicroTaskQueue @ zone.js @ 0.6.12?main =浏览器:494ZoneTask.invoke @ [email protected]?main =浏览器:426
platform-b​​rowser.umd.js:962 STACKTRACE:BrowserDomAdapter.logError @ platform-b​​rowser.umd。 js:962ExceptionHandler.call @ core.umd.js:3698(匿名函数)@ core.umd.js:8951schedulerFn @core.umd.js:6007SafeSubscriber .__ tryOrUnsub @ Subscriber.ts:240SafeSubscriber.next @ Subscriber.ts:192Subscriber。 _next @ Subscriber.ts:133Subscriber.next @ Subscriber.ts:93Subject._finalNext @ Subject.ts:154Subject._next @ Subject.ts:144Subject.next @ Subject.ts:90EventEmitter.emit @ core.umd.js:5996onError @ core.umd.js:6227onHandleError @ core.umd.js:6096ZoneDelegate.handleError @ [email protected]?main = browser:327Zone.runGuarded @ [email protected]?main = browser:233_loop_1 @ [email protected]?main = browser:487drainMicroTaskQueue @ [email protected]。 12?main =浏览器:494ZoneTask.invoke @ [email protected]?main =浏览器:426
platform-b​​rowser.umd.js:962错误:未捕获(在承诺中):[object Object]
at resolvePromise([email protected]?main = browser:538)
at resolvePromise([email protected]?main = browser:523)
at [email protected] .12?main =浏览器:571
在ZoneDelegate.invokeTask([email protected]?main = browser:356)
at Object.onInvokeTask(core.umd.js:6066)
在ZoneDelegate.invokeTask([email protected]?main = browser:355)
在Zone.runTask([email protected]?main = browser:256)
在drainMicroTaskQueue ([email protected]?main = browser:474)
,XMLHttpRequest.ZoneTask.invoke([email protected]?main = browser:426)BrowserDomAdapter.logError @ platform-b​​rowser.umd。 js:962ExceptionHandler.call @ core.umd.js:3699(匿名函数)@ core.umd.js:8951sch edulerFn @ core.umd.js:6007SafeSubscriber .__ tryOrUnsub @ Subscriber.ts:240SafeSubscriber.next @ Subscriber.ts:192Subscriber._next @ Subscriber.ts:133Subscriber.next @ Subscriber.ts:93Subject._finalNext @ Subject.ts:154Subject。 _next @ Subject.ts:144Subject.next @ Subject.ts:90EventEmitter.emit @core.umd.js:5996onError @ core.umd.js:6227onHandleError @ core.umd.js:6096ZoneDelegate.handleError @ [email protected]。 12?main =浏览器:327Zone.runGuarded @ [email protected]?main =浏览器:233_loop_1 @ [email protected]?main =浏览器:487drainMicroTaskQueue @ [email protected]?main=browser: 494ZoneTask.invoke @ [email protected]?main = browser:426
[email protected]?main = browser:461未处理承诺拒绝:响应{_body:对象,状态:404,确定: false,statusText:Not Found,header:Headers ...};区域:角;任务:Promise.then;值:响应{_body:对象,状态:404,ok:false,statusText:未找到,标题:标题...} consoleError @ [email protected]?main =浏览器:461_loop_1 @ [email protected]。 12?main = browser:490drainMicroTaskQueue @ [email protected]?main = browser:494ZoneTask.invoke @ [email protected]?main = browser:426
[email protected]?main =浏览器:463错误:未捕获(承诺):[对象](...)

任何帮助将不胜感激。

Any help would be much appreciated.

推荐答案

我刚从其他论坛和示例中了解到我们应该使用jsonp来访问跨域休息。
,例如

I just got to know from other forums and examples that we should use jsonp to access crossdomain rests.e.g. http://plnkr.co/edit/0dFCzRdmBwcT99WR5Lbd?p=preview

@Component

@Component

import {JSONP_PROVIDERS}  from 'angular2/http';
...
..
 providers:[JSONP_PROVIDERS]

在服务中:

import {Jsonp, URLSearchParams} from 'angular2/http';

这篇关于Angular2 http获取请求结果为404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:04