Axios无法发布导航器getCurrentPosition

问题描述

我只是想发布用户的当前位置,我可以控制台记录返回的值

{
    "coords": {
        "accuracy": 3628,"altitude": null,"altitudeAccuracy": null,"heading": null,"latitude": ---,"longitude": ---,"speed": null
    },"timestamp": 1597499108300
}

但是一旦axios尝试将值发布到API,该值就会更改为{}

无论值的键名称如何,都会发生这种情况,例如,我可以放置"fakekeyname": location或其他任何内容,然后将值更改为{}

<html>
    <head>
        <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    </head>

    <body>
        <script>
            function start() {
                navigator.geolocation.getCurrentPosition(
                    (location) => {
                        console.log(location)

                        axios.post("http://localhost:1337/v1/posts",{
                            location,})
                    },(error) => console.error(error)
                )
            }
        </script>
    </body>
</html>

解决方法

似乎问题在于返回的“对象”实际上是由吸气剂定义的,这就是为什么它们不能与axios帖子一起使用的原因。来源是此评论和我发现的堆栈溢出帖子:

我猜是因为您没有将其转换为普通对象。您是否尝试过提取需要通过POST请求发送的数据? 例如:{lat:location.coords.latitude,lng:location.coords.longitude}

Why spread syntax can't work in navigator's position object?