ionic 3标头请求未在后发方法中发送

时间:2018-08-02 06:14:03

标签: http ionic-framework ionic3 ionic-native

我正在开发一个用于发送带有标头的json数据请求的应用程序,还可以接收带有标头的json数据的响应。 响应的接收工作正常,但是请求发送中的报头未发送。添加标题时,响应显示错误。  当我使用

let header = new HttpHeaders();
header = header.set('abc', '22');
alert(header.get('abc'));

    this.http.post(this.apiUrl+'/login', {postParams}, {header}).then(data => {
        console.log(data.status);
        console.log(data.data); // data received by server
        console.log(data.headers);
    var json = JSON.stringify(data.headers);
    console.log(JSON.stringify(data.headers["x-auth-token"]));

})
.catch(error => {

    console.log(error.status); // showing as "undefined"
    console.log(error.error); // showing as "undefined"
    console.log(error.headers); // showing as "undefined"
});

使用此代码时,它在错误部分显示“未定义”

但是没有标题就可以工作

this.http.post(this.apiUrl+'/login', {postParams}, {header}).then(data => {

响应也获得标题。

我正在使用HTTP。 https://ionicframework.com/docs/native/http/

完整代码如下所示。

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { HTTP } from '@ionic-native/http';
import { RestProvider } from '../providers/rest/rest';

@NgModule({
    declarations: [
    MyApp,
    HomePage
],
imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
    entryComponents: [
        MyApp,
        HomePage
],
providers: [
    StatusBar,
    SplashScreen,
    HTTP,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    RestProvider
]
})
export class AppModule {}

**rest.ts**

import { Injectable } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Headers, RequestOptions  } from '@angular/http';
import { HTTP } from '@ionic-native/http';
@Injectable()
export class RestProvider {

    apiUrl = 'http://192.168.0.11:5005/ksjp/api';
constructor(private http: HTTP) {
    console.log('Hello RestProvider Provider');
}

getPost(data) {     
    return new Promise(resolve => {
let header = new HttpHeaders();
header = header.set('abc', '22');
console.log(header.get('abc'));  // display the value '22'

    let postParams = data;
        this.http.post(this.apiUrl+'/login', {postParams}, {header})
.then(data => {
    console.log(data.status);
    console.log(data.data); // data received by server
    console.log(data.headers);
    var json = JSON.stringify(data.headers);
})
.catch(error => {
    console.log(error.status);
    console.log(error.error); // error message as string
    console.log(error.headers);
});

});

}

}

我尝试了其他方法,但是没有用。请帮助我。

2 个答案:

答案 0 :(得分:0)

我面临着同样的问题。

您可以尝试添加标题,如下所示:

    let headers = {
      "Content-type": 'application/xml',
      "Authorization": token,
    };

并添加标头对象

this.http.post(url, xmlBody, headers).then((response) => {}

这样做,您的标头将随您的请求一起发送。

希望这会有所帮助。

答案 1 :(得分:0)

直接在请求中添加标题时我得到了答案。

 this.http.post(this.apiUrl+'/login', {postParams}, {"Content-Type": "application/json","auth":"anbnvbnhvbnbnvbnvbnbnbn"})

这个为我工作