Ionic 2 - Angular 2 http Headers不随请求一起发送 [英] Ionic 2 - Angular 2 http Headers are not being sent along with the request
问题描述
我正在使用Ionic的最新测试版,我已经为我的api服务器做了一个http post方法。但标题不会随请求一起发送。我使用的代码如下:
**离子版 - Beta-8& Angular版本-rc.3
I am working in the latest beta release of Ionic and I have done a http post method to my api server. But the headers are not being sent along with the request. The code that i have used is as below : ** Ionic version - Beta-8 & Angular version -rc.3
import {Page,App,NavParams} from 'ionic-angular';
import {Headers, Http, RequestOptions} from '@angular/http';
import {Component} from '@angular/core';
import 'rxjs/add/operator/map';
@Component({
templateUrl : 'build/pages/xyz/xyz.html'
})
export class Xyz{
form:any;
token:any;
constructor(public app:App, navParams:NavParams, public http:Http){
let code = {abc : 'abc'};
let headers = new Headers();
let body = JSON.stringify(code);
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Bearer ' + "tokenContent");
let options =new RequestOptions({headers : headers, body:body});
this.http.post('http://myserver/myapi', options)
.map(res => res.json())
.subscribe(
data=>{
console.log(data.message);
},
err=>{
console.log(err);
},
()=>{
console.log("Process Complete");
}
);
当我查看console.log选项对象和标题时,标题设置正确。但是当我发出http请求时,当我将它们包含在options对象中时,标题和正文都没有被发送。但是当我尝试单独发送身体时,我能够在请求有效载荷中看到它。
When I look at console.log both options object and headers, the headers are set properly. But when I make the http request both the headers and body is not being sent when I enclose them in the options object. But when I try to send the body alone I am able to see it in the request payload.
推荐答案
如果您从浏览器进行测试,它将无效。请查看与文档相关的CORS请求。
It's not working if you test from browser.. Please check the documentation related CORS requests.
http://blog.ionic.io/处理 - cors-issues-in-ionic /
这篇关于Ionic 2 - Angular 2 http Headers不随请求一起发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!