Angular 6:SyntaxError:JSON中的意外标记O位于JSON.parse的位置0处,具有有效的JSON

Angular 6:SyntaxError:JSON中的意外标记O位于JSON.parse的位置0处,具有有效的JSON,json,angular,http-post,Json,Angular,Http Post,我不确定我的函数是否错误(应该向服务器发送数据),或者服务器上的函数是否错误。 我查了一些类似的问题,但我对问题采取的解决方案不起作用 我的函数如下所示: postData(number){ let array = JSON.stringify(this.locArr); return this.http.post<any>(this.url, array) .subscribe(), error => console.log("Error: ",

我不确定我的函数是否错误(应该向服务器发送数据),或者服务器上的函数是否错误。 我查了一些类似的问题,但我对问题采取的解决方案不起作用

我的函数如下所示:

postData(number){
   let array = JSON.stringify(this.locArr);
   return this.http.post<any>(this.url, array)
    .subscribe(),
    error => console.log("Error: ", error)
    }
所有参数,如token等,都由服务器函数接收,但是我上面写的数组是空的,尽管它是有效的json。 我想知道为什么会发生这种错误


任何建议都值得赞赏

本地数组将通过Angular自动转换为JSON,您无需对其进行字符串化或解析

postData(number){
    this.http.post<any>(this.url, this.locArr)
    .subscribe((data)=>{
        //code after receiving data from server
    },
        error => console.log("Error: ", error))
}
postData(数字){
this.http.post(this.url,this.locArr)
.订阅((数据)=>{
//从服务器接收数据后的代码
},
error=>console.log(“error:,error))
}

我相信您使用的是httpClientModule,因此您不需要
JSON.stringify
删除此步骤
JSON.stringify(this.locArr)
您还需要将其作为json对象
{}
而不是json数组
[]

postData($locArr){ // pass the array to the service function
   let data = { data : $locArr}; // note that you have to post it as json object {} not []
   return this.http.post<any>(this.url,data);
}
postData($locArr){//将数组传递给服务函数
让data={data:$locArr};//注意,必须将其作为json对象{}而不是[]发布
返回this.http.post(this.url,数据);
}

此json无效。请检查您的json:对不起,我更改了,忘记移除昏迷。现在它应该是有效的json。另外,您正在向服务器发布一个数组,该数组不是有效的json或更像文本。你应该把它作为json对象发布,我不知道你的意思。上面的数组是有效的json?我用验证器检查了它,或者你是说别的什么?我用Object.assign尝试了它,并将它作为JSON对象发送,但它仍然不起作用。@Cara请检查一下嘿,谢谢你的回复!我认为这是服务器端错误。我要试试看!
postData($locArr){ // pass the array to the service function
   let data = { data : $locArr}; // note that you have to post it as json object {} not []
   return this.http.post<any>(this.url,data);
}