Angular2 http获取请求结果变成404

2024-03-09

尝试在 Angular 2 + TypeScript 中运行简单的 http get 调用 给予https://angular.io/docs/ts/latest/tutorial/toh-pt6.html https://angular.io/docs/ts/latest/tutorial/toh-pt6.html

here: https://angular.io/resources/live-examples/toh-6/ts/plnkr.html https://angular.io/resources/live-examples/toh-6/ts/plnkr.html

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

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

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

完全异常

EXCEPTION: Error: Uncaught (in promise): [object Object] platform-browser.umd.js:962 EXCEPTION: Error: Uncaught (in promise): [object Object]BrowserDomAdapter.logError @ platform-browser.umd.js:962BrowserDomAdapter.logGroup @ platform-browser.umd.js:972ExceptionHandler.call @ core.umd.js:3696(anonymous function) @ 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] /cdn-cgi/l/email-protection?main=browser:327Zone.runGuarded @ [email protected] /cdn-cgi/l/email-protection?main=browser:233_loop_1 @ [email protected] /cdn-cgi/l/email-protection?main=browser:487drainMicroTaskQueue @ [email protected] /cdn-cgi/l/email-protection?main=browser:494ZoneTask.invoke @ [email protected] /cdn-cgi/l/email-protection?main=browser:426 platform-browser.umd.js:962 STACKTRACE:BrowserDomAdapter.logError @ platform-browser.umd.js:962ExceptionHandler.call @ core.umd.js:3698(anonymous function) @ 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] /cdn-cgi/l/email-protection?main=browser:327Zone.runGuarded @ [email protected] /cdn-cgi/l/email-protection?main=browser:233_loop_1 @ [email protected] /cdn-cgi/l/email-protection?main=browser:487drainMicroTaskQueue @ [email protected] /cdn-cgi/l/email-protection?main=browser:494ZoneTask.invoke @ [email protected] /cdn-cgi/l/email-protection?main=browser:426 platform-browser.umd.js:962 Error: Uncaught (in promise): [object Object] at resolvePromise ([email protected] /cdn-cgi/l/email-protection?main=browser:538) at resolvePromise ([email protected] /cdn-cgi/l/email-protection?main=browser:523) at [email protected] /cdn-cgi/l/email-protection?main=browser:571 at ZoneDelegate.invokeTask ([email protected] /cdn-cgi/l/email-protection?main=browser:356) at Object.onInvokeTask (core.umd.js:6066) at ZoneDelegate.invokeTask ([email protected] /cdn-cgi/l/email-protection?main=browser:355) at Zone.runTask ([email protected] /cdn-cgi/l/email-protection?main=browser:256) at drainMicroTaskQueue ([email protected] /cdn-cgi/l/email-protection?main=browser:474) at XMLHttpRequest.ZoneTask.invoke ([email protected] /cdn-cgi/l/email-protection?main=browser:426)BrowserDomAdapter.logError @ platform-browser.umd.js:962ExceptionHandler.call @ core.umd.js:3699(anonymous function) @ 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] /cdn-cgi/l/email-protection?main=browser:327Zone.runGuarded @ [email protected] /cdn-cgi/l/email-protection?main=browser:233_loop_1 @ [email protected] /cdn-cgi/l/email-protection?main=browser:487drainMicroTaskQueue @ [email protected] /cdn-cgi/l/email-protection?main=browser:494ZoneTask.invoke @ [email protected] /cdn-cgi/l/email-protection?main=browser:426 [email protected] /cdn-cgi/l/email-protection?main=browser:461 Unhandled Promise rejection: Response {_body: Object, status: 404, ok: false, statusText: "Not Found", headers: Headers…} ; Zone: angular ; Task: Promise.then ; Value: Response {_body: Object, status: 404, ok: false, statusText: "Not Found", headers: Headers…}consoleError @ [email protected] /cdn-cgi/l/email-protection?main=browser:461_loop_1 @ [email protected] /cdn-cgi/l/email-protection?main=browser:490drainMicroTaskQueue @ [email protected] /cdn-cgi/l/email-protection?main=browser:494ZoneTask.invoke @ [email protected] /cdn-cgi/l/email-protection?main=browser:426 [email protected] /cdn-cgi/l/email-protection?main=browser:463 Error: Uncaught (in promise): [object Object](…)

任何帮助将非常感激。


我刚刚从其他论坛和示例中知道我们应该使用 jsonp 来访问跨域休息。 例如http://plnkr.co/edit/0dFCzRdmBwcT99WR5Lbd?p=preview http://plnkr.co/edit/0dFCzRdmBwcT99WR5Lbd?p=preview

@成分

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

服务中:

import {Jsonp, URLSearchParams} from 'angular2/http'; 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular2 http获取请求结果变成404 的相关文章

  • mat-tab-group 不是 Angular 9 中的已知元素

    我正在使用 Angular 9 和 Angular Material 9 2 4 我正在尝试使用mat tab 组在我的 component html 中 但我不断收到错误 mat tab group is not a known elem
  • 如何禁用 HTTP 的 HSTS 标头?

    我已将以下内容插入到我网站的 htaccess 中 以便能够访问HSTS预加载列表 https hstspreload appspot com
  • Angular 2 - 加载时共享服务

    我正在尝试将服务共享给组件 为此 我创建了 service ts 其中代码如下 import Subject from rxjs Subject export class CommonService CommonList new Subje
  • 使用特定 HTTP 方法链接到页面 (DELETE)

    如何像 Rails 那样链接到页面并让浏览器使用 DELETE 方法调用它 我试过 a href DELETE ME a 但不起作用 我使用 Node js 所以我可以用它来处理 DELETE 方法 你不能 链接只会触发 GET 请求 您可
  • Webpack 在 Angular 的 ng 服务中的作用

    我是 Angular 的新手 想知道当我们为应用程序提供服务时 Webpack 在幕后扮演什么角色 在最初的印象中 我开始知道 webpack 是一个构建和打包工具 它将所有必需的 JS 文件分组到单独的包中 然而 我无法找到 webpac
  • 如何在 Angular2 中为表单分配和验证数组

    我的模型 this profile 在 javascript 中有一个属性叫做emails 这是一个数组 email isDefault status 然后我将其定义如下 this profileForm this formBuilder
  • 如何在 ngrx/effects 中执行 if/else 操作?

    我正在使用 ngrx effects 我想根据以下情况分派不同的操作foo商店里的状态 这就是我现在正在做的 Effect foo1 this updates whenAction Actions FOO filter obj gt obj
  • 角度订阅响应

    好吧 我对 Angular 还很陌生 所以我遇到了这个小问题 所以我遵循 Angular 指南 https angular io guide http https angular io guide http 所以我的问题是我的 http r
  • Angular4 - 滚动到锚点

    我正在尝试对同一页面上的锚元素进行简单的滚动 基本上 用户点击 尝试 按钮 它就会滚动到页面下方 ID 为 登录 的区域 现在 它正在与一个基本的id login a href login a 但它正在跳转到该部分 理想情况下 我希望它滚动
  • 如何在 Angular 2 应用程序中从 TypeScript/JavaScript 中的字符串获取类?

    在我的应用程序中 我有这样的内容 user ts export class User 现在 我这样做 应用程序组件 ts callAnotherFunction User 如果我将类名作为字符串 即 我该如何做到这一点 User 如果可能的
  • 如何在 TypeScript 中禁用/抑制库中的错误?

    我打开了一些编译器开关来报告代码中的更多问题 例如严格的空检查 但我在使用的库中遇到了数十个错误 例如 default xxx node modules angular core src util decorators d ts 11 5
  • MatAutocomplete 值 X 显示

    我的自动完成显示具有以下定义的对象的值 export class Person id number name string cityName string 这是自动完成模板
  • HttpWebRequest vs Webclient(特殊场景)

    我知道这个问题之前已经回答过thread https stackoverflow com questions 1694388 webclient vs httpwebrequest httpwebresponse 但我似乎找不到详细信息 在
  • 415 不支持的媒体类型; Angular2 到 API

    我是 Angular 2 的新手 我面临着一个无法找到解决方案的问题 当我尝试从 Angular 2 发布到 API 时 我得到 415 不支持的媒体类型 角度2代码 onSubmit value any console log value
  • 如何显示 GroupList 的 FormArray?

    我正在尝试制作一个交互式表单 在每一行上列出一个项目以及一个删除按钮 在我的示例中称为 verwijderen 这些项目是从数据库中检索的 并且每个项目都实例化为名为的自定义对象LaborPeriod 然后这些对象被转化为FormGroup
  • Angular2 Router:将主题标签添加到 url

    我正在使用 Angular2 Seed 应用程序 您可以在官方仓库 https github com mgechev angular2 seed 正如您所看到的 这里我们导入了 angular2 router 并且我们使用它来创建应用程序的
  • ASP.NET 中 HTTP 缓存相关标头的有效含义

    我正在 ASP NET 2 0 中开发一个 Web 应用程序 其中涉及通过资源处理程序 ashx 提供图像 我刚刚实现了处理缓存标头和条件 GET 请求 这样我就不必为每个请求提供所有图像 但我不确定我是否完全理解浏览器缓存发生了什么 图像
  • 在java中轮询Http服务器(重复发送http get请求)

    当对其进行 REST 调用时 我的 Web 服务器会发送一些信息 我想不断轮询该服务器 间隔5秒后重复发送HTTP GET请求 以检查返回的信息是否有任何变化 做到这一点最有效的方法是什么 您能提供一些代码示例吗 请注意 我只想开发客户端代
  • $http.get() 与 JSON 数据

    我正在编写一个服务器应用程序 并希望客户端使用正文中的数据来参数化我的 GET 方法 如下所示 http v GET http localhost 3000 url text 123 foo bar GET url HTTP 1 1 Acc
  • 将 Angular Web 组件 EventEmitter 监听到 javascript

    我在以下工具的帮助下创建了一个小型网络组件本文 https medium com IMM9O web components with angular d0205c9db08f使用角度元素 其中包括 Input and Output 我能够将

随机推荐