相关疑难解决方法(0)

使用带有角度2的http rest apis

所以,我正在通过打字稿在他们的网站上关注角度2指南,并且我陷入了http api集成.我正在尝试创建一个简单的应用程序,可以通过soundcloud api搜索一首歌,但是我很难实现和理解如何开始,而且在线资源以很多不同的方式完成它(我相信快速的角度2语法更改)早些时候).

所以目前我的项目看起来像这样

app
  components
    home
      home.component.ts
      ...
    search
      search.component.ts
      ...
    app.ts
    ...
  services
    soundcloud.ts
  bootstrap.ts
index.html
Run Code Online (Sandbox Code Playgroud)

在示例中没有任何花哨的东西,主要文件将是

app.ts

import {Component, View} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';

import {HomeComponent} from './home/home.component';
import {SearchComponent} from './search/search.component';

@Component({
    selector: 'app',
    templateUrl: 'app/components/app.html',
    directives: [ROUTER_DIRECTIVES]
})

@RouteConfig([
  {path: '/home', name: 'Home', component: HomeComponent, useAsDefault: true},
  {path: '/search', name: 'Search', component: SearchComponent}
])

export class App { }
Run Code Online (Sandbox Code Playgroud)

bootstrap.ts

    import {App}     from './components/app';
import {bootstrap}        from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router'; …
Run Code Online (Sandbox Code Playgroud)

javascript http angular

32
推荐指数
2
解决办法
5万
查看次数

catch不适用于http.get Angular2

我正在尝试使用Http服务进行ajax调用,这工作正常.

现在我想处理失败案件.例如,如果我们得到404或任何其他错误.

我使用以下代码.

import {Component,Inject} from '@angular/core';
import {Http, Response,HTTP_PROVIDERS} from '@angular/http';
import {DoService} from './service';
import 'rxjs/Rx';
import 'rxjs/add/operator/catch';

@Component({
    selector: 'comp-one',
    template: `<h2>My First Angular 2 App</h2>{{data}}
        <input type="button" (click)="doSomething()" value="Do Http"/>`
})

export class ComponentOne { 
    constructor(public _http:Http){
    console.log("It is from ComponentOne constructor..");
        this._http = _http;
    }
    data:Object;
    doSomething(){
        console.log("It is from doSomething ComponentOne");
        let temp:any = this._http.get('people.json')//people.json is not exist, intendedly maing any error
     .map(res => res.json())
        .catch((err:any)=>{ console.log("Something is wrong..")});// If i keep this line …
Run Code Online (Sandbox Code Playgroud)

angular2-services angular

7
推荐指数
2
解决办法
9722
查看次数

Angular2 http响应正文出错

this.http.put(url, data)
    .map(response => response.json())
    .subscribe(
      response => console.log(response),
      error => console.log(error),
    );
Run Code Online (Sandbox Code Playgroud)

成功后,它会输出API返回的数据.出错时输出为ProgressEvent,状态为0.

发生错误时如何从API获取响应数据?

angular2-http angular

7
推荐指数
1
解决办法
5715
查看次数

Angular2捕获请求错误

我有一个代码来获取请求.它正在编译和工作,但我在控制台中有错误.

public get<T>(url: string, headers: Headers = this.jsonHeaders()): Observable<T> {
        return this._http.get(url, { headers: headers })
                   .catch((err: Response, caught: Observable<T>) => {
                       if (err.status === 401) {
                           this._router.navigate(["/auth/login"]);
                           return Observable.throw("401 Unauthorized");
                       }
                       return caught;
                   })
                   .map(res => <T>this.toJSON(res));
    }
Run Code Online (Sandbox Code Playgroud)

错误:

error TS2345: Argument of type '(err: Response, caught: Observable<T>) => ErrorObservable | Observable<T>' is not assignable to parameter of type '(err: any, caught: Observable<Response>) => Observable<any>'.
  Types of parameters 'caught' and 'caught' are incompatible.
    Type 'Observable<Response>' is not assignable to …
Run Code Online (Sandbox Code Playgroud)

ajax request rxjs rxjs5 angular

3
推荐指数
1
解决办法
1960
查看次数