簡體   English   中英

angular2 http帖子默默地失敗了

[英]angular2 http post failing silently

我的前端檢查后端以查看訪客模型是否存在。 此調用使用postman( POSTlocalhost:1337/visitor/exists with data: {'email': 'some@email.com'} )。 當我嘗試讓我的angular2服務進行相同的調用時,它會無聲地失敗。

這是我的服務:

@Injectable()
export class MyService {
  private myUrl = 'localhost:1337/visitor/exists';

  constructor(private http: Http) { }

  checkVisitor(email :string): Observable<boolean> {
    console.log('in myservice, checkvisitor; email: ', email); // this outputs

    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    let body = {'email': email};

    console.log('body, ', body); // this also outputs

    return this.http.post(this.myUrl, JSON.stringify(body), options)
      .map(this.extractData)
      .catch(this.handleError);

  }

 private extractData(res: Response) {
    console.log('in service, extractData; res: ', res); // this does not print
    let body = res.json();
    return body || { };
  }

 private handleError (error: Response | any) {
    console.log('in handleError'); // this does not print
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }
}

為什么我不能從后端得到回復?

我在我的組件中調用它:

constructor(private myService : MyService){
}
...
checkEmailUniqueness(fieldTouched){
    if(fieldTouched){
      this.myService.checkVisitor(this.visitor.email)
    }
  }

默認情況下,Observable是“冷”的,您需要subscribe它們才能“觸發”它們。

例:

this.myService.checkVisitor(this.visitor.email).subscribe((response)=>{
   console.log(response);
})

試試這種方式:

 import { Http, Headers, Response, URLSearchParams } from '@angular/http';     

@Injectable()
export class MyService {
  private myUrl = 'localhost:1337/visitor/exists';
private globalHeaders: Headers = new Headers();
  constructor(private http: Http) { }

  checkVisitor(email :string): Observable<boolean> {
    console.log('in myservice, checkvisitor; email: ', email); // this outputs


    let body = {'email': email};

    console.log('body, ', body); // this also outputs

    return this.http.post(this.myUrl, JSON.stringify(body), {headers: this.globalHeaders})
      .map(this.extractData)
      .catch(this.handleError);

  }

 private extractData(res: Response) {
    console.log('in service, extractData; res: ', res); // this does not print
    let body = res.json();
    return body || { };
  }

 private handleError (error: Response | any) {
    console.log('in handleError'); // this does not print
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM