简体   繁体   English

如何将嵌套主体添加到 fetch()

[英]How to add nested body to fetch()

How to add nested body to fetch() function?如何将嵌套主体添加到fetch() function?

I have this body:我有这个身体:

{
                "locations": [
                    {
                        "latLng": {
                            "lat": 123,
                            "lng": 123
                        }
                    },
                    {
                        "latLng": {
                            "lat": 123,
                            "lng": 123
                        }
                    }
                ]
            }

I try with this code, but it returns me SyntaxError: Unexpected token o in JSON at position 1 :我尝试使用此代码,但它返回我SyntaxError: Unexpected token o in JSON at position 1

fetch(url, {
        method: "post",
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: {
                "locations": [
                    {
                        "latLng": {
                            "lat": 53.438679,
                            "lng": 14.520770
                        }
                    },
                    {
                        "latLng": {
                            "lat": 53.450439,
                            "lng": 14.536400
                        }
                    }
                ]
            }
    });

The body of the init object for fetch can be one of: a Blob , BufferSource , FormData , URLSearchParams , USVString (string), or ReadableStream object.用于fetch的 init object 的body可以是以下之一: BlobBufferSourceFormDataURLSearchParamsUSVString (字符串)或ReadableStream object。 ( MDN | spec ) MDN |规范

You're not giving it any of those things.你没有给它任何这些东西。 You're giving it a plain object.你给它一个普通的 object。 If you want to send JSON , you need to give it a string:如果你想发送JSON ,你需要给它一个字符串:

fetch(url, {
    method: "post",
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
// −−−−−−−^^^^^^^^^^^^^^^
            "locations": [
                {
                    "latLng": {
                        "lat": 53.438679,
                        "lng": 14.520770
                    }
                },
                {
                    "latLng": {
                        "lat": 53.450439,
                        "lng": 14.536400
                    }
                }
            ]
        })
// −−−−−−^
});

The reason you're getting the specific error you are is that since what you've provided isn't one of the things it expects, fetch is converting it to string as though you did String(yourObject) .您收到特定错误的原因是,由于您提供的内容不是它所期望的内容之一,因此fetch正在将其转换为字符串,就像您String(yourObject)一样。 On a plain object, unless you've done something to override it (which you haven't in your example), the result is "[object Object]" , which is invalid JSON as of the o in object after the opening [ .在一个普通的 object 上,除非你已经做了一些事情来覆盖它(你在你的例子中没有这样做),结果是"[object Object]" ,这是无效的 JSON 在object之后的o [


Remember: JSON is a textual notation for data exchange.请记住:JSON 是用于数据交换的文本符号 (More here.) If you're dealing with JavaScript source code, and not dealing with a string , you're not dealing with JSON. (更多在这里。)如果您正在处理 JavaScript 源代码,而不是处理字符串,那么您就不是在处理 JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM