【问题标题】:Http.Request(new Request({})) sent twiceHttp.Request(new Request({})) 发送两次
【发布时间】:2016-02-25 07:21:52
【问题描述】:

我正在使用 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());
    }

}

【问题讨论】:

  • 没有看到调用HttpProvider.httpRequ(...)的代码就无法判断。
  • 请检查网络标签。你会发现preflight option 的一个请求就像@GünterZöchbauer 在回答中添加的一样

标签: http request angular response


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 2021-02-11
    相关资源
    最近更新 更多