How do I make an HTTP request in Javascript?
- User
How do I make an HTTP request in Javascript?
- Assistant
To make an HTTP request in JavaScript, you can use the XMLHttpRequest object or the Fetch API. The Fetch API is more modern and easier to use, so I'll give an example using that.
Here's how you can make an HTTP GET request using the Fetch API:
fetch('https://api.example.com/data') .then(response => { if (!response.ok) { throw new Error('Network response was not ok'); } return response.json(); }) .then(data => { console.log(data); }) .catch(error => { console.error('There was a problem with the fetch operation:', error); });And here's an example of an HTTP POST request using the Fetch API:
const data = { key1: 'value1', key2: 'value2' }; fetch('https://api.example.com/data', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }) .then(response => { if (!response.ok) { throw new Error('Network response was not ok'); } return response.json(); }) .then(data => { console.log(data); }) .catch(error => { console.error('There was a problem with the fetch operation:', error); }); - User
Please write a SWOT analysis for Thai government.
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.