该回答引用GPTᴼᴾᴱᴺᴬᴵ
在浏览器控制台中,您可以使用fetch()方法来发送GET和POST请求。以下是使用fetch()方法发送GET和POST请求的示例代码:
1.发送GET请求:
fetch('https://example.com/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
在这个示例中,我们使用fetch()方法发送一个GET请求来获取来自https;//example.com/data的数据。我们通过.then()方法和.catch()方法来处理响应和错误。最后,我们将响应数据记录到控制台中。
2.发送POST请求:
fetch('https://example.com/api/data', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'John Doe',
age: 30
})
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
请注意,您需要将URL替换为您要访问的实际URL,并根据需要更改请求头和请求正文。如果您在发送请求时遇到任何问题,请检查网络连接和浏览器控制台中的错误消息。