【问题标题】:Error: advanced-http value must be string Ionic错误:高级 http 值必须是字符串 Ionic
【发布时间】:2019-03-09 21:48:26
【问题描述】:

我尝试使用 @angular/http 发送我的 api,但出现 401 polyfills 错误,因此我将其更改为 @ionic-native/http ,但随后又出现另一个错误:Error: advanced-http值必须是字符串”,所以我记录了我的标题,它是空的???

日志

"normalizedNames": {},
  "lazyUpdate": [
    {
      "name": "Authorization",
      "value": "Basic xxxxxxxx",
      "op": "a"
    },
    {
      "name": "Content-Type",
      "value": "application/x-www-form-urlencoded",
      "op": "a"
    }
  ],
  "headers": {},
  "lazyInit": {
    "normalizedNames": {},
    "lazyUpdate": null,
    "headers": {}
  }

我的代码

import { HTTP } from '@ionic-native/http';

 constructor(public http: HTTP) {}

  login(username, password) {

    let body = {
      username: username,
      password: password
    };
    let headers = new HttpHeaders();
    headers = headers.append('Authorization', 'Basic xxxxxx');
    headers = headers.append('Content-Type', 'application/x-www-form-urlencoded');
return this.http.post(apiUrl, body, { headers: headers })
  .then(data => {

    console.log(data.status);
  console.log(data.data); // data received by server
    console.log(data.headers);

  })
  .catch(error => {

   console.log(error); // error message as string

  });

【问题讨论】:

    标签: api http ionic-framework ionic3


    【解决方案1】:

    试试这个。

    将此添加到您的 app.module 中

    import { HttpClientModule } from '@angular/common/http';
    
    imports: [
      BrowserModule,
      HttpClientModule,
      IonicModule.forRoot(MyApp)
    ],
    

    然后在你的代码中

    //import { HTTP } from '@ionic-native/http';
    import { HttpClient } from '@angular/common/http';
    
     constructor(public http: HttpClient) {}
    
      login(username, password) {
    
        let body = {
          username: username,
          password: password
        };
        let headers = new HttpHeaders();
        headers = headers.append('Authorization', 'Basic xxxxxx');
        headers = headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this.http.post(apiUrl, body, { headers: headers })
      .subscribe(data => {
    
        console.log(data.status);
      console.log(data.data); // data received by server
        console.log(data.headers);
    
      })
      .catch(error => {
    
       console.log(error); // error message as string
    
      });
    

    【讨论】:

    • 我使用的是 HttpClient,但它对我不起作用HTTP 正常状态。
    【解决方案2】:

    线程很旧,但可能可以帮助其他人。更改您的标题,如下所示并进行测试。

    return this.http.post(apiUrl, body, { headers: 'Content-Type: application/json', Authorization: 'Basic xxx'})....
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-23
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多