如何从 fetch post 请求中检索响应数据?

问题描述 投票:0回答:1

我正在尝试显示从服务器返回的数据,当我查看 Chrome 开发者窗口中的网络选项卡时,我可以看到从我正在访问的服务器返回的数据。

如何在下面的代码中访问它?

单击按钮时将调用handleClick 方法。

谢谢!

let data;
async function handleClick() {
        const url = "http://127.0.0.1:8000/parse"
         data  = await fetch(url, {
            mode: 'no-cors',
            method: "POST",
            headers: { 'Content-type': 'application/x-www-form-urlencoded'},
            body: new URLSearchParams({
                locale: "en_GB",
                text: "Test Message"
            })
        })
        .then(x => x.text())
        console.log(data)
    }
javascript fetch-api svelte
1个回答
0
投票
 // Capture in the promise return section

.then(x => {
   // Catch the data through the propertiesName. 
   // Here properties means your post request's variable name which you are given.
   console.log(x.propertiesName);
});
© www.soinside.com 2019 - 2024. All rights reserved.