Javascript获取主体JSON列表

我正在尝试发出帖子请求并收到此异常:

"unsupported BodyInit type"

我认为问题在于请求的主体。 phoneNumbers采用phoneNumbers = ["1234567890","1234567891"]的形式(即字符串列表)。我尝试将JSON.stringify(phoneNumbers)用作正文,但是即使列表不为空,也似乎返回“ []”。

 export async function findUsersByPhoneNumbersnotFollowing(userId,phoneNumbers) {
  const reqConfig = {
    method: 'POST',headers: {
      'Content-Type': 'application/json',accept: 'application/json',},body: phoneNumbers,};


  const response = await authfetch(`${API_URL}/following/recommendations/${userId}`,reqConfig);

  if (response.error) {
    throw new Error(response.error);
  }
  return response;
}

我要去哪里错了? API端点期望List<String>(使用spring框架,并且controller方法在带注释的@RequestBody中使用此参数)

lyp6197 回答:Javascript获取主体JSON列表

尝试发送JSON对象而不是普通数组:

    const reqConfig = {
        method: 'POST',headers: {
          'Content-Type': 'application/json',Accept: 'application/json',},body: JSON.stringify({
            paramName: phoneNumbers
        }),};

paramName替换为您在API端点上期望的名称。

本文链接:https://www.f2er.com/3149595.html

大家都在问