Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-11-20· OpenAIAi2 WildChat-4.8M (AI2)

buatkan saya kode javascript untuk axios html

  1. User

    buatkan saya kode javascript untuk axios html

  2. Assistant

    Tentu! Berikut adalah contoh kode JavaScript sederhana menggunakan library axios untuk membuat permintaan HTTP (GET dan POST) dan kemudian menampilkan HTML respon.

    1. Instalasi Axios

    Pastikan terlebih dahulu Anda memiliki axios di proyek Anda. Anda dapat menginstalnya dengan cara:

    Menggunakan npm:

    npm install axios
    

    Atau 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 axios dalam 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:

    1. Library Axios dimuat dari CDN agar dapat digunakan tanpa instalasi langsung.
    2. 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.
    3. Respon dari server (baik GET maupun POST) ditampilkan di elemen <div> dengan ID content.
    4. Error ditangani menggunakan blok try-catch agar tidak terjadi crash.

    Output:

    1. Jika Anda klik tombol GET Request, data dari server dummy akan ditampilkan, seperti Title dan Body.
    2. Jika Anda klik tombol POST Request, response server akan menampilkan ID dari data yang berhasil dikirim.

    Cobalah langsung dengan file ini di browser! 😃

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.