Angular2:尝试使用休息服务从服务器获取Json时出现奇怪的模糊错误

时间:2016-12-05 07:45:37

标签: rest angular

我正在尝试使用angular2 rest服务从本地服务器获取产品。当我执行GET时,我收到以下错误。我可以使用Insomnia rest客户端从服务器获取项目,所以我知道问题不在于服务器。我还检查了网址并确保那里没有错误。知道我还能检查什么吗?我使用ng-cli来运行应用程序...

我的错误:

http://10.60.160.34/BRMServices/WebEnquiry//POSEnquiry/293
Failed to load resource: net::ERR_CONNECTION_RESET
app.component.ts:32
failureServer error

我的休息服务:

import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions  } from "@angular/http";
import { Observable } from  "rxjs/Rx";
import { ProductModel } from "../models/product.model";

//import 'rxjs/add/operator/map';
//import 'rxjs/add/operator/catch';
//import 'rxjs/add/observable/throw' 

@Injectable()
export class RestService {
  public API_URL: string = "http://10.60.160.34/BRMServices/WebEnquiry/";
  private headers: Headers;
  private options: RequestOptions;

  constructor(private http: Http){
    this.init();
  }

  init() {
    this.headers = new Headers({ 'Content-Type': 'application/json' });
    this.options = new RequestOptions({ headers: this.headers });
  }

  getProduct(barcode: string): Observable<ProductModel> {
    return this.http.get(this.API_URL + "/POSEnquiry/" + barcode, this.options)
    .map((res: Response) => res.json())
    .catch((error: any) => Observable.throw(error.json().error || 'Server error'));
  }  
}

我的app.component.ts:

import { Component } from '@angular/core';
import { RestService } from "./services/rest.service";
import { ProductModel } from "./models/product.model";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  product: ProductModel;
  constructor(private restService: RestService){}

  submitBarcode(barcode: HTMLInputElement){
    this.restService.getProduct(barcode.value)
    .subscribe((res) => {
      //product = res;
      console.log(res);
    }, (res) => {
      console.log("failure" + res);
    });
    //console.log("product: " + product);
  }
}

1 个答案:

答案 0 :(得分:0)

原来是RequestOptions变量。当我在get请求中用标题替换选项时,它没有任何问题......