像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"}]

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


回答:

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

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

console.log(res)

})


回答:

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

以上是 像JSON格式给接口接收的问题? 的全部内容, 来源链接: utcz.com/p/934730.html

回到顶部