CONVERTIR CODIGO A JQUERY AJAX GET EJEMPLO SELECT OPTION

 hacer este codigo con el metodo ajax get de jquery <!DOCTYPE html><html lang="es-ES"><head> <meta charset="utf-8">
<title>Json & Jquery</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body> <h1>Json & Jquery</h1>
<select id="usuarios"></select> 

<script type="text/javascript">
$(document).on('ready',function (){ $.getJSON('usuarios.json', function(data) { $.each(data, function(key, value) { $("#usuarios").append('<option name="' + key + '">' + value + '</option>'); }); // close each() }); 
});
</script>
</body></html>
Mostrar versiones











I'd be glad to provide the code using jQuery's AJAX GET method to fetch data from a JSON file and populate a dropdown list:

HTML
<!DOCTYPE html>
<html lang="es-ES">
<head>
  <meta charset="utf-8">
  <title>Json & Jquery</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>  </head>
<body>
  <h1>Json & Jquery</h1>
  <select id="usuarios"></select>

  <script type="text/javascript">
    $(document).ready(function() {
      $.ajax({
        url: 'usuarios.json', // Replace with the actual URL of your JSON file
        type: 'GET',
        dataType: 'json',
        success: function(data) {
          $.each(data, function(key, value) {
            $("#usuarios").append('<option value="' + key + '">' + value + '</option>');
          });
        },
        error: function(jqXHR, textStatus, errorThrown) {
          console.error("Error fetching data:", textStatus, errorThrown);
          // Handle errors gracefully, e.g., display an error message to the user
        }
      });
    });
  </script>
</body>
</html>
Usa el código con precaución.
Explanation:

Include jQuery: The <script> tag references the latest jQuery library (https://jquery.com/). Update the version if needed.
Document Ready: The $(document).ready(function() { ... }) ensures the code executes after the DOM is fully loaded.
AJAX Request: The $.ajax function is used to make an asynchronous HTTP request:
url: The URL of your JSON file (replace with the actual path).
type: Set to 'GET' for retrieving data.
dataType: Set to 'json' to indicate that the response is JSON.
Success Handling:
The success callback function is executed if the request is successful.
data: The response data, which is a JavaScript object in this case (parsed from JSON).
It iterates through the data using $.each and appends an <option> element to the #usuarios select element for each key-value pair:
value: Sets the option's value to the key.
Content: Sets the option's text content to the value.
Error Handling:
The error callback function is executed if the request encounters an error.
It logs the error details to the console using console.error.
You can add error handling logic here, such as displaying an error message to the user.
Important Notes:

Replace 'usuarios.json' with the actual URL of your JSON file.
Ensure your JSON file is accessible from the HTML page, considering cross-origin restrictions.
Consider using a more recent jQuery version for better performance and compatibility.

Comentarios

Entradas populares de este blog

Como proteger una api json con php al pasar el parametro POST[id] de los ataques sql injection y xss

Copia de Seguridad de Archivo MS Access (.mdb) a Pendrive con VB6

NUMEROS SERIE DE DISCO RIGIDO Y PENDRIVER USB CON PHP