繁体   English   中英

无法使用角度发送帖子请求

[英]unable to send post request using angular

我正在使用HTTP post发送表单数据,就像这样

     saveDataFile(mutlidata,id,value): Observable<Response> {

        var _url = 'http://xxxx.xxx.xxx';
        var saveDataURL = _url + '/' + id;
        var _this = this;

        let headers = new Headers();
        headers.append('Content-Type', 'multipart/form-data');
        let options = new RequestOptions({ headers: headers });
        const frmData = new FormData();
        frmData.append("file",JSON.stringify(mutlidata),'sample.json');
        frmData.append("dataCheck",value);


        return this.http.post(saveDataURL,frmData, options).pipe(
            map((res: Response) => {
                 return res;
            }),
            catchError(this.handleError),);


    }

这里有多数据

    [["fakepath/test1.JPG","ea305e-be9d"],["fakepath/test2.JPG","489ce580-c50e-40e6-b1ab-71c7827f636c"]]

    id will have 15asdas6asd6

    value will have  either true / false

这里它不发送我猜的表单数据,当我调试和检查frmdata返回this.http.post(saveDataURL,frmData,options)时,它显示form.entries,values等

在sample.json文件中,我附加了formdata

frmData.append("file",JSON.stringify(mutlidata),'sample.json');

这里的文件意味着我必须使用该关键字将数据发送到API

无需附加Content-Type ,因为浏览器会为您处理它。 我最后一次尝试手动将其添加到我的帖子请求时,它导致我的HTTP请求失败。 您可以删除部分代码。

saveDataFile(mutlidata,id,value): Observable<Response> {

  var _url = 'http://xxxx.xxx.xxx';
  var saveDataURL = _url + '/' + id;
  var _this = this;

  const frmData = new FormData();
  frmData.append("file",JSON.stringify(mutlidata),'sample.json');
  frmData.append("dataCheck",value);

  return this.http.post(saveDataURL, frmData, options).pipe(
    map((res: Response) => {
         return res;
    }),
    catchError(this.handleError),);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM