像JSON格式给接口接收的问题?

代码如下

const sampleIds =this.params.idList
const newarr = [] as any
for (let i = 0; i < sampleIds.length; i++) {
    var item = { sampleId: sampleIds[i] } 
    newarr.push(item)
}
this.postGaugingV3Unbound({ data: { sampleIdList: JSON.stringify(newarr)}}).then(res => {
   console.log(res)
})

打印出 JSON.stringify(newarr) 是这样的格式:
[{"sampleId":"1158684648019372539"},{"sampleId":"1158684648019372539"}]

可是接口接收如下图,这样的格式就不对了
image.png
期望以正常 json 格式给后台 [{"sampleId":"1158684648019372539"}] 这样格式

阅读 2.3k
2 个回答

去除 JSON.stringify() 直接发送 newarr

this.postGaugingV3Unbound({ data: { sampleIdList: newarr}}).then(res => {
   console.log(res)
})

传输的方式是对的,但是后端并不一定把接收到的参数重新解析。有可能直接就当成有效对象或者集合使用了。
所以解决方案一个是让后端解析接收到的参数,一个是你在请求接口的时候不要增加 JSON.stringify() 这个转换操作。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题