2016-12-05 66 views
0

我試圖從使用angular2 rest服務的本地服務器獲取產品。當我執行GET時,出現以下錯誤。我可以使用Insomnia休息客戶端從服務器獲取項目,所以我知道問題不在服務器中。我也檢查了網址,並確保沒有錯誤。任何想法我可以檢查什麼?我使用的NG-CLI運行應用程序...Angular2:嘗試使用休息服務從服務器獲取Json時出現奇怪的模糊錯誤

我的錯誤:

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); 
    } 
} 

回答

0

原來是RequestOptions變量。當我在取得請求中用標題替換選項時,它沒有問題...

相關問題