【发布时间】: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(...)的代码就无法判断。 -
请检查网络标签。你会发现
preflightoption的一个请求就像@GünterZöchbauer 在回答中添加的一样
标签: http request angular response