简体   繁体   English

角 10 | http 帖子 | 字符串数组追加 FormData

[英]Angular 10 | Http post | String array append FormData

I have to make a post request to an api endpoint, but I get an error status 500.我必须向 api 端点发出 post 请求,但收到错误状态 500。

name: "HttpErrorResponse"
ok: false
status: 500
statusText: "Internal Server Error"

This is my code:这是我的代码:

var selectedIds = ["31"];
let sendData = new FormData();
sendData.append('auth', this.dataService.REG_AUTH);
sendData.append('identifier', identifier);
sendData.append('selected[]', selectedIds); 

this.http.post<any>('APIENDPOINT', sendData).subscribe(data => {
  console.log(data);
}, error => {  
  console.log(error);
});

The issue is in this line: sendData.append('selected[]', selectedIds);问题出在这一行: sendData.append('selected[]', selectedIds); I have no clue how to pass an array to FormData.我不知道如何将数组传递给 FormData。

This is a working example from our android app.这是来自我们的 android 应用程序的一个工作示例。 I need to convert this request in angular/typescript syntax:我需要以 angular/typescript 语法转换此请求:

@JvmSuppressWildcards
@FormUrlEncoded
@POST("APIENDPOINT")
fun addData(
    @Field("auth") auth: String,
    @Field("identifier") identifier: String,
    @Field("selected[]") selected: ArrayList<String>
): Call<ResponseBody>

What I know so far:到目前为止我所知道的:

It seems angular does not serialize the data, so I tried some hardcoded fixes, but none of these worked:似乎 angular 不会序列化数据,所以我尝试了一些硬编码修复,但这些都没有奏效:

sendData.append('selected%5B%5D', '%2231%22'); 
sendData.append('selected%5B%5D', '31');
sendData.append('selected%5B%5D', 31);
sendData.append('selected%5B%5D', '%5B%2231%22%5D'); 
sendData.append('selected%5B%5D', selectedIds); 
sendData.append('selected%5B%5D', JSON.stringify(selectedIds));

If I use selected instead of selected[] , then I get no error, but obviously no data is updated, so I am pretty sure it is a serialization/parsing issue.如果我使用selected而不是selected[] ,那么我不会出错,但显然没有数据更新,所以我很确定这是一个序列化/解析问题。

From this answer :这个答案

FormData's append() method can only accept objects of string or blob type. FormData 的 append() 方法只能接受 string 或 blob 类型的对象。 If you need to append the array, use JSON.stringify() method to convert your array into a valid JSON string.如果需要附加数组,请使用 JSON.stringify() 方法将数组转换为有效的 JSON 字符串。

formData.append('selected[]', JSON.stringify(selectedIds));

The statusCode 500 is the Internal Server Error, and it's the server-side problem. statusCode 500是内部服务器错误,它是服务器端问题。 So, It's better to check the API if it can receive your request or not.因此,最好检查API是否可以接收您的请求。

FormData's append() method accept string or blob type So you can use JSON.stringify() method ( formData.append('selectedIds', JSON.stringify(selectedIds)); ). FormData 的append()方法接受stringblob类型因此您可以使用JSON.stringify()方法( formData.append('selectedIds', JSON.stringify(selectedIds)); )。 So try this:所以试试这个:

let selectedIds = ["31"];
let sendData = new FormData();
sendData.append('auth', this.dataService.REG_AUTH);
sendData.append('identifier', identifier);
sendData.append('selectedIds', JSON.stringify(selectedIds)); 

this.http.post<any>('APIENDPOINT', sendData).subscribe(data => {
  console.log(data);
}, error => {  
  console.log(error);
});

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

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