typescript 如何在打字稿中为数组使用 formData.append

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44991758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:42:07  来源:igfitidea点击:

How use formData.append for a array in typescript

angulartypescript

提问by Steven Daniel Anderson

Hi i want to send a form to my endpoint Profile, my problem is in the field user:{}, because i can't find the way to put my array into this field.

您好,我想向我的端点配置文件发送一个表单,我的问题出在字段 user:{} 中,因为我找不到将数组放入该字段的方法。

this are the field in my endpoint:

这是我端点中的字段:

{
  "id": 4,
  "ci": "123456",
  "photo": "http://127.0.0.1:8000/media/profiles/12809632_10208569440535095_617453747387788113_n_zAUAVMf.jpg",
  "phone_number": "+59177621589",
  "user": {
    "id": 5,
    "username": "sdanderson",
    "first_name": "ssss",
    "last_name": "ssss"
  },
  "experience": "null",
  "typeskill": [
    {
      "id": 1,
      "skill_name": "developer"
    }
  ]
}

And here is my service for make a PUT request:

这是我提出 PUT 请求的服务:

putProfile(id:string,token:string,body:any,files:any):Observable<Profile>{

//save data to send to the endpoint for update
    let formData: FormData = new FormData();

    for (let file of files) {
        formData.append('photo', file);
    }
    formData.append('ci',body['ci']);  
    formData.append('phone_number', body['phone_number']); 
    formData.append('experience',body['experience']);
    formData.append('user',body['user']);//here i have inside the fields: body['user'].id,body['user'].first_name,body['user'].last_name





    //include header
    let headers = new Headers();
    headers.append('Accept', 'application/json');
    headers.append("Authorization","Token "+ token);

    return this.http.put(this.api+"profile/"+id+'/',formData,{headers})
        .map(this.extractData)
        .catch(this.handleError);
}

回答by myroslav

FormData's append()method can only accept objects of stringor blobtype. If you need to append the array, use JSON.stringify()method to convert your array into a valid JSON string.

FormData 的append()方法只能接受stringblob类型的对象。如果需要附加数组,请使用JSON.stringify()方法将数组转换为有效的 JSON 字符串。

Here is how:

方法如下:

formData.append('user', JSON.stringify(body['user']));

Hereyou can read more about JavaScript's JSON object.

在这里您可以阅读有关 JavaScript 的 JSON 对象的更多信息。

回答by Pawe? Rymsza

I had similar problem and I had declare array in key. For your example it would looks like this:

我有类似的问题,我在键中声明了数组。对于您的示例,它看起来像这样:

formData.append('user[id]', body['user']['id']);
...

It can be usefull if you wanten't or you can't parse json on server site

如果您不想或无法在服务器站点上解析 json,它会很有用

回答by seescode

Instead of using FormData try using a plain typescript object:

不要使用 FormData 尝试使用普通的打字稿对象:

putProfile(id:string,token:string,body:any,files:any):Observable<Profile>{

    //save data to send to the endpoint for update
    let formData = {
      "id": 4,
      "ci": body['ci'],
      "photo": file,
      "phone_number": body['phone_number'],
      "user": {
        "id": body['user'].id,
        "username": "body['user'].username,
        "first_name": body['user'].first_name,
        "last_name": body['user'].last_name
      },
      "experience": body['experience'],
      "typeskill": [
        {
          "id": 1,
          "skill_name": "developer"
        }
      ]
    }   

    //include header
    let headers = new Headers();
    headers.append('Accept', 'application/json');
    headers.append("Authorization","Token "+ token);

    return this.http.put(this.api+"profile/"+id+'/',formData,{headers})
        .map(this.extractData)
        .catch(this.handleError);
}