Ionic 3项目和base64映像中的http post中的data参数

时间:2018-09-29 21:29:04

标签: angular http ionic3

我有一个ionic 3项目,我正在尝试发布请求。到目前为止,我一直在使用以下运行良好的代码:

import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import {HttpParams} from '@angular/common/http';
import { Observable, Subscription } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/Rx';

@Injectable()
export class Need4CarGateway {
  token = "";
  availabilityPeriod="00:00";
  private sub: Subscription;


  constructor(public http: Http, private logger:Logger,private utils:Utils) {
  }


  myPostRequest(){
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded' );
    headers.append('Authorization', 'Bearer '+mytoken);
    headers.append('access_token', ACCESSTOKEN );
    let options = new RequestOptions({ headers: headers });

    let body = [
      {key: 'first_name',     value: firstNameVariable},
      {key: 'last_name',    value: lastNameVariable}
    ].map(x => `${encodeURI(x.key)}=${encodeURI(x.value)}`).join('&');

    return this.http.post(myurl, body, options)
          .retry(NUM_HTTP_RETRIES)
          .map((res: Response) => res.json())
          .toPromise();
  }

}

但是,当我尝试将base64图像(即

)传递给我的参数时,我遇到了问题
  myPostRequest(){
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded' );
    headers.append('Authorization', 'Bearer '+mytoken);
    headers.append('access_token', ACCESSTOKEN );
    let options = new RequestOptions({ headers: headers });

    let body = [
      {key: 'first_name',     value: firstNameVariable},
      {key: 'last_name',    value: lastNameVariable},
      {key: 'myIdFile',    value: my64baseImgString},
    ].map(x => `${encodeURI(x.key)}=${encodeURI(x.value)}`).join('&');

    return this.http.post(myurl, body, options)
          .retry(NUM_HTTP_RETRIES)
          .map((res: Response) => res.json())
          .toPromise();
  }

在这种情况下,到达映像的服务器已损坏。我不确定,因为主体变量是否应采用first_name = AName&last_name = LastName&myIdFile = 64baseImg的形式,因此字符串很长。

我一直在寻找可以传递body参数的另一种方法。我试图将其写为json对象,即。

let body = {
  first_name:firstNameVariable,
  last_name:lastNameVariable,
  myIdFile:my64baseImgString
};

或作为HttpParams,即

  let body = new HttpParams();
    body.append('first_name',firstNameVariable);
    body.append('last_name',lastNameVariable);
    body.append('myIdFile',my64baseImgString);

我一直在环顾四周,看来body变量必须是一个对象,但是在我看来,它不能按预期工作。如果将其作为对象,我会不断出现http错误。在邮递员中,如果我按要求的方式放置标题和正文参数,即使用键和值,一切都可以正常工作。

我在做什么错了?

谢谢

1 个答案:

答案 0 :(得分:0)

不确定这是否是最正确的答案,但对我有用。实际上,您可以发送json对象,即

let body = {
  first_name:firstNameVariable,
  last_name:lastNameVariable,
  myIdFile:my64baseImgString
};

如果您在标头请求中提到内容是json对象,即

  myPostRequest(){
    var headers = new Headers();
    headers.append('Content-Type', 'application/json' );
    //instead of 
    //headers.append('Content-Type', 'application/x-www-form-urlencoded' );
    headers.append('Authorization', 'Bearer '+mytoken);
    headers.append('access_token', ACCESSTOKEN );
    let options = new RequestOptions({ headers: headers });

    let body = {
      first_name:firstNameVariable,
      last_name:lastNameVariable,
      myIdFile:my64baseImgString
    };

    return this.http.post(myurl, body, options)
          .retry(NUM_HTTP_RETRIES)
          .map((res: Response) => res.json())
          .toPromise();
  }

但是服务器需要接受Json对象并允许CORS(跨域资源共享)。