am_dot_com

SW 2022-05-13

May 13th, 2022 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. <!-- owmap_example.HTML -->
  2. <!-- exemplo de utilização de ums serviço terceiro, externo, por pedido assíncrono -->
  3. <!DOCTYPE html>
  4. <html lang="en">
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>openweathermap.org - Exemplo de pedido</title>
  8. <script src="owmap_example.js"></script>
  9. </head>
  10. <body>
  11. <h1>Exemplo de pedido à API de "Weather" de
  12. <a href="https://openweathermap.org">https://openweathermap.org</a>
  13. </h1>
  14. <form>
  15. <label for="idLocal">O nome de uma localização:</label>
  16. <select id="idSelectPredefinedLocations">
  17. <option>Santarém, Portugal</option>
  18. <option>Lisboa, Portugal</option>
  19. <option selected>Santiago do Chile, Chile</option>
  20. </select>
  21. <br>
  22. <input
  23. type="text"
  24. id="idLocal"
  25. placeholder="Cidade, País">
  26. <input type="button" id="idBtnObterResposta" value="obter resposta">
  27. </form>
  28. <hr>
  29. <section id="idSectionFeedback"></section>
  30. </body>
  31. </html>
  32.  
  33.  
  34. //owmap_example.js
  35.  
  36. window.onload = boot
  37.  
  38. const ID_SELECT_PREDEFINED_LOCALS = "idSelectPredefinedLocations",
  39. ID_BTN_OBTER_RESPOSTA = "idBtnObterResposta",
  40. ID_LOCAL = "idLocal",
  41. ID_SECTION_FEEDBACK = "idSectionFeedback"
  42.  
  43. var oSelectPreDefLocals, oBtnObterResposta, oLocal, oSFeedback
  44.  
  45. function id(pId){return document.getElementById(pId)}
  46.  
  47. function boot(){
  48. oSelectPreDefLocals = id(ID_SELECT_PREDEFINED_LOCALS)
  49. oBtnObterResposta = id(ID_BTN_OBTER_RESPOSTA)
  50. oLocal = id(ID_LOCAL)
  51. oSFeedback = id(ID_SECTION_FEEDBACK)
  52.  
  53. oBtnObterResposta.onclick = obterResposta
  54. }//boot
  55.  
  56. const OWMAP_BASE_URL =
  57. "https://api.openweathermap.org/data/2.5/weather?"
  58.  
  59. function buildRequest(
  60. pLocal,
  61. pApiKey=MY_KEY
  62. ){
  63. var strLocal = encodeURIComponent(pLocal)
  64. var strReq = `${OWMAP_BASE_URL}q=${strLocal}&appid=${pApiKey}`
  65. return strReq
  66. }//buildRequest
  67.  
  68. function obterResposta(){
  69. //fabricar o pedido
  70. var strIdLocal = oLocal.value.trim()
  71. var strLocal = strIdLocal==="" ? oSelectPreDefLocals.value : strIdLocal
  72. var strReq=buildRequest(strLocal) //URL
  73.  
  74. //mostra apenas uma a que permite executar o pedido
  75. oSFeedback.innerHTML = `<a href="${strReq}">${strReq}</a>`
  76.  
  77. //enviar o pedido + visualizar a resposta a pedido
  78. ajaxGet(
  79. strReq,
  80. oSFeedback
  81. )
  82. }//obterResposta
  83.  
  84. function ajaxGet(
  85. pUrlReq, //API endpoint
  86. pWhereToReply,
  87. pAsync=true
  88. ){
  89. var oReq = new XMLHttpRequest()
  90. if(oReq){
  91. oReq.open(
  92. "GET",
  93. pUrlReq,
  94. true
  95. )
  96. oReq.onreadystatechange = respondeMudancaNaMaturidadeDoPedido
  97. oReq.mWhereToReply = pWhereToReply
  98. oReq.send(null)
  99.  
  100. return true
  101. }
  102. else{
  103. return false
  104. }
  105. }//ajaxGet
  106.  
  107. function respondeMudancaNaMaturidadeDoPedido(){
  108. var bResponseReady = this.readyState===4 && this.status===200
  109. if(bResponseReady){
  110. this.mWhereToReply.innerHTML = this.responseText
  111. this.oResponse = json.parse(this.responseText)
  112. }
  113. else{
  114. this.mWhereToReply.innerHTML+=`Current state: ${this.readyState}<br>`
  115. }
  116. }//respondeMudancaNaMaturidadeDoPedido
Add Comment
Please, Sign In to add comment