Http.Request(新请求({}))发送两次

时间:2016-02-25 07:21:53

标签: http request angular response

我正在使用http.Request方法对服务器进行http调用。在浏览器控制台中,我看到两个http请求。一个响应为POST,GET,HEAD,XHR显示为状态200,另一个给出正确的响应,但XHR状态显示为其他。 http.Request在服务中,订阅在组件类中。

服务类是

export class Httpprovider {
    http: Http;
    constructor(http: Http){
        this.http = http;
    }
    httpReq(url: string, method: string, data: any, header: Headers){
        let headers = new Headers();
        headers.append('Content-Type', 'application/json');
        console.log(headers);

        if (method === 'GET'){ var methods = RequestMethod.Get} 
        else if (method === 'POST'){ var methods = RequestMethod.Post}
        else if (method === 'PUT'){var methods = RequestMethod.Put}
        else if (method === 'PATCH'){var methods = RequestMethod.Patch} 
        else if (method === 'DELETE'){var methods = RequestMethod.Delete}
        else {methods = RequestMethod.Get};

        return this.http.request(new Request({
                    method: methods,
                    url: url,
                    body: JSON.stringify(data),
                    headers: headers
                })).map(res => res.json());
    }

}

1 个答案:

答案 0 :(得分:2)

要么多次订阅HttpProvider.httpRequ(...).subscribe(),要么是CORS预检(类型为OPTIONS AFAIR),因此它可能是前者。