gpt-4o-2024-11-20· OpenAI
WildChat-4.8M (AI2)
buatkan saya kode javascript untuk axios html
- User
buatkan saya kode javascript untuk axios html
- Assistant
Tentu! Berikut adalah contoh kode JavaScript sederhana menggunakan library
axiosuntuk membuat permintaan HTTP (GET dan POST) dan kemudian menampilkan HTML respon.1. Instalasi Axios
Pastikan terlebih dahulu Anda memiliki
axiosdi proyek Anda. Anda dapat menginstalnya dengan cara:Menggunakan npm:
npm install axiosAtau langsung gunakan CDN di dalam
<script>jika tanpa Node.js:<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
2. Kode JavaScript menggunakan Axios
Berikut contoh penggunaan
axiosdalam kode HTML:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Axios HTML Example</title> <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> </head> <body> <h1>Axios Example</h1> <div id="content">Waiting for response...</div> <button id="get-button">GET Request</button> <button id="post-button">POST Request</button> <script> // Get references to the buttons and content div const contentDiv = document.getElementById('content'); const getButton = document.getElementById('get-button'); const postButton = document.getElementById('post-button'); // Handle GET request getButton.addEventListener('click', async () => { contentDiv.innerText = 'Fetching data...'; try { const response = await axios.get('https://jsonplaceholder.typicode.com/posts/1'); contentDiv.innerHTML = ` <h2>GET Response</h2> <p><strong>Title:</strong> ${response.data.title}</p> <p><strong>Body:</strong> ${response.data.body}</p> `; } catch (error) { contentDiv.innerText = `Error: ${error.message}`; } }); // Handle POST request postButton.addEventListener('click', async () => { contentDiv.innerText = 'Sending data...'; try { const response = await axios.post('https://jsonplaceholder.typicode.com/posts', { title: 'My New Post', body: 'This is the content of the new post!', userId: 1 }); contentDiv.innerHTML = ` <h2>POST Response</h2> <p><strong>ID:</strong> ${response.data.id}</p> <p><strong>Title:</strong> ${response.data.title}</p> <p><strong>Body:</strong> ${response.data.body}</p> `; } catch (error) { contentDiv.innerText = `Error: ${error.message}`; } }); </script> </body> </html>
Penjelasan Kode:
- Library Axios dimuat dari CDN agar dapat digunakan tanpa instalasi langsung.
- Dua tombol dibuat untuk melakukan permintaan HTTP:
- GET Request ke endpoint
https://jsonplaceholder.typicode.com/posts/1. - POST Request dengan data JSON ke endpoint
https://jsonplaceholder.typicode.com/posts.
- GET Request ke endpoint
- Respon dari server (baik
GETmaupunPOST) ditampilkan di elemen<div>dengan IDcontent. - Error ditangani menggunakan blok
try-catchagar tidak terjadi crash.
Output:
- Jika Anda klik tombol
GET Request, data dari server dummy akan ditampilkan, seperti Title dan Body. - Jika Anda klik tombol
POST Request, response server akan menampilkan ID dari data yang berhasil dikirim.
Cobalah langsung dengan file ini di browser! 😃
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.