Ionic 2 - Angular 2 http Headers 没有随请求一起发送
Ionic 2 - Angular 2 http Headers are not being sent along with the request
我正在使用 Ionic 的最新 Beta 版,我已经对我的 api 服务器执行了一个 http post 方法。但是 headers 没有随请求一起发送。我使用的代码如下:
** 离子版本 - Beta-8 & Angular 版本 -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 选项 object 和 headers 时,headers 设置正确。但是当我发出 http 请求时,当我将 headers 和 body 包含在选项 object 中时,它们都没有被发送。但是当我尝试单独发送 body 时,我能够在请求负载中看到它。
如果您从浏览器进行测试,则无法正常工作。请查看与 CORS 请求相关的文档。
这应该适合您,因为 http.post
的第二个参数是正文:
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Bearer ' + "tokenContent");
let options = new RequestOptions({ headers: headers });
this.http.post('http://myserver/myapi', body, options)
.map(...
首先,检查这个问题是不是CORS的问题。
另外,试试下面的代码:
let headers = new Headers();
headers.append('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
this.http.post(`${this.link}`,'sRequest=' + sRequest1,{ headers: headers });
我正在使用 Ionic 的最新 Beta 版,我已经对我的 api 服务器执行了一个 http post 方法。但是 headers 没有随请求一起发送。我使用的代码如下: ** 离子版本 - Beta-8 & Angular 版本 -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 选项 object 和 headers 时,headers 设置正确。但是当我发出 http 请求时,当我将 headers 和 body 包含在选项 object 中时,它们都没有被发送。但是当我尝试单独发送 body 时,我能够在请求负载中看到它。
如果您从浏览器进行测试,则无法正常工作。请查看与 CORS 请求相关的文档。
这应该适合您,因为 http.post
的第二个参数是正文:
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Bearer ' + "tokenContent");
let options = new RequestOptions({ headers: headers });
this.http.post('http://myserver/myapi', body, options)
.map(...
首先,检查这个问题是不是CORS的问题。
另外,试试下面的代码:
let headers = new Headers();
headers.append('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
this.http.post(`${this.link}`,'sRequest=' + sRequest1,{ headers: headers });