Post请求中使用字符串

466 阅读1分钟

前端在与后端通过body传递数据的时候,多数情况下都是JSON格式;

还有一些其他格式,比如直接在body传递字符串,实现代码如下:

主要需要data使用JSON.stringify, headers需要加上请求头设置content-type

 return apiRequest(`/api/url`, {
    method: 'post',
    data: JSON.stringify(params),
    headers: { 'Content-Type': 'application/json;charset=UTF-8' },
  })