如何将数组数据作为formdata angular 4发送 [英] how to sent array data as formdata angular 4

查看:216
本文介绍了如何将数组数据作为formdata angular 4发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发布未发送到服务器的数据数组:

I tried to post an array of data is not sending to server:

网络服务:

 deleteCategory() {
    return  this.http.post('http://www.demo/webapi/deletecategory', { 
        headers: {
          "Authorization": "Token " + this.token,
          "Content-Type": "application/x-www-form-urlencoded"
        },
        withCredentials: true
      }
      )
    }

在ts文件中

onDelete() {
      this.userService.deleteCategory().subscribe(response => {
      this.selectedArray = [];
     for (var i = 0; i< this.selection._selected.length; i++){
     this.selectedArray.push(this.selection._selected[i].category_id) ;
     console.log(' selected value:', this.selectedArray);

     }
    })
      }

以html

<button class="btn-danger pull-right" (click)="onDelete()" type="button"  >Delete</button>

推荐答案

现在,您的deleteCategory()方法正在使用空的请求正文向指定的端点发出POST请求.

Right now your deleteCategory() method is making a POST request to the specified endpoint with an empty request body.

这是一个简单的示例,说明如何使用Angulars HttpClient在POST请求中包含对象数组

Here is a simple example on how to include an array of objects in a POST request using Angulars HttpClient

this.http.post('some url', JSON.stringify(yourArrayOfObjects), {headers: ...})

如果您想发送FormData(例如,如果您正在将文件上传到服务器)

If you wanted to send FormData (if for example you're uploading a file to a server)

 const frmData = new FormData();
 frmData.append("fileUpload", file)
 this.http.post('some url', frmData, {headers:...})

这篇关于如何将数组数据作为formdata angular 4发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆