Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
¿Cómo obtengo datos en formato JSON desde IBM Cloud con una solicitud HTTP utilizando JavaScript?

Cuando hago clic en "OBTENER DATOS" en mi aplicación, me gustaría acceder a los datos en mi IBM Cloud con una solicitud HTTP. Necesito los datos en formato JSON. Esto debe implementarse con JavaScript. Mi código actual está aquí:

 function httpRequest() { const xhr = new XMLHttpRequest() //open a get request with the remote server URL xhr.open("GET", "https://<orgID>.internetofthings.ibmcloud.com/api/v0002/device/types/<typeID>/devices/<deviceID>/state/<logicalInterfaceID>" ) //send the Http request xhr.send() //EVENT HANDLERS //triggered when the response is completed xhr.onload = function() { if (xhr.status === 200) { //parse JSON datax`x data = JSON.parse(xhr.responseText) console.log(data.count) console.log(data.products) } else if (xhr.status === 404) { console.log("No records found") } } //triggered when a network-level error occurs with the request xhr.onerror = function() { console.log("Network error occurred") } //triggered periodically as the client receives data //used to monitor the progress of the request xhr.onprogress = function(e) { if (e.lengthComputable) { console.log(`${e.loaded} B of ${e.total} B loaded!`) } else { console.log(`${e.loaded} B loaded!`) } } }
 .btn { cursor: pointer; background-color: #555; color: #fff; display: inline-block; padding: 5px; margin-left: auto; margin-right: auto; }
 <!DOCTYPE html> <html lang="de"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="src/css/styles.css"/> <script src="src/js/script.js"></script> <title>GET DATA</title> <div class="btn" onclick="httpRequest()"> GET DATA </div> </head> <body> </body> </html>

El marcador de posición orgID , typeID , deviceID , logicalInterfaceID en mi código, etc., por supuesto, ha sido reemplazado por la ID correcta.

El problema es que no sé cómo incluir el nombre de usuario y la contraseña en la URL para poder acceder a IBM Cloud.

https://www.ibm.com/docs/en/mapms/1_cloud?topic=reference-application-rest-apis

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Echa un vistazo a esta publicación sobre ¿Qué está haciendo realmente la bandera -u en cURL?

Puede codificar en base64 sus credenciales utilizando la función nativa btoa() y luego establecerlas en el encabezado de solicitud de autorización.

 var xhr = new XMLHttpRequest(); xhr.open( "GET", "https://<orgID>..."); xhr.setRequestHeader("Authorization", `Basic ${btoa('username:password')}`); xhr.send();
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!