2016-12-05 65 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变量。当我在取得请求中用标题替换选项时,它没有问题...

相关问题