am_dot_com

CN 2022-05-13

May 13th, 2022 (edited)
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. <!-- ex_owm.html -->
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Exemplo de pedido a openweathermap.org</title>
  7. <script src="ex_owm.js"></script>
  8. </head>
  9. <body>
  10. <h1>Fazer pedido a openweathermap.org</h1>
  11. <form>
  12. <label for="idLocal">Indique a localização: </label>
  13. <input type="text"
  14. id="idLocal"
  15. placeholder="cidade, País">
  16. <br>
  17. <select id="idSelectPredefinedLocals">
  18. <option>Santarém, Portugal</option>
  19. <option>Lisboa, Portugal</option>
  20. <option selected>Santiago do Chile, Chile</option>
  21. </select>
  22. <input type="button"
  23. id="idBtnFazerPedido"
  24. value="Fazer pedido">
  25. </form>
  26. <hr>
  27. <section id="idSectionFeedback"></section>
  28. </body>
  29. </html>
  30.  
  31. ***********************+
  32.  
  33. //ex_owm.js
  34. window.onload = boot
  35.  
  36. //endpoint
  37. const API_BASE_URL =
  38. "https://api.openweathermap.org/data/2.5/weather?"
  39.  
  40. const ID_LOCAL ="idLocal",
  41. ID_SELECT_PRE_DEFINED_LOCALS = "idSelectPredefinedLocals",
  42. ID_BTN_FAZER_PEDIDO = "idBtnFazerPedido",
  43. ID_SECTION_FEEDBACK = "idSectionFeedback"
  44.  
  45. var oLocal, oSelectPredefinedLocals, oBtnFazerPedido, oSectionFeedback
  46.  
  47. function id(pId){return document.getElementById(pId)}
  48.  
  49. function boot(){
  50. //assocs
  51. oLocal = id(ID_LOCAL)
  52. oSelectPredefinedLocals = id(ID_SELECT_PRE_DEFINED_LOCALS)
  53. oBtnFazerPedido = id(ID_BTN_FAZER_PEDIDO)
  54. oSectionFeedback = id(ID_SECTION_FEEDBACK)
  55.  
  56. //behaviors
  57. oBtnFazerPedido.onclick = obterRespostaOWM
  58. }
  59.  
  60. /*
  61. clientes de API
  62. síncronos?
  63. assíncronos?
  64. XMLHttpRequest
  65. GET
  66. POST => FormData
  67. */
  68. function buildRequestUrl(
  69. pLocal,
  70. pApiKey=MY_KEY
  71. ){
  72. var strLocal = encodeURIComponent(pLocal)
  73. var strReq = `${API_BASE_URL}q=${strLocal}&appid=${pApiKey}`
  74. //http://base-da-api?q=Santarem,Portugal&appid=20392039
  75. return strReq
  76. }//buildRequestUrl
  77.  
  78. function obterRespostaOWM(){
  79. var strLocal = oLocal.value.trim()!="" ? oLocal.value : oSelectPredefinedLocals.value
  80.  
  81. //preparar o endereço correspondente ao pedido GET
  82. var strUrl =
  83. buildRequestUrl(
  84. strLocal
  85. ) //q=Santarém,Portugal&appid=239203920392
  86.  
  87. //#1 - fornece a possibilidade de se fazer o pedido manualmente, via URL
  88. oSectionFeedback.innerHTML = `<a href='${strUrl}'>${strUrl}</a>`
  89.  
  90. //#alt #2 - executar o pedido
  91. pedidoGet(
  92. strUrl,
  93. oSectionFeedback
  94. )
  95.  
  96. //submeter o pedido + visualizar a resposta
  97. }//obterRespostaOWM
  98.  
  99. function pedidoGet(
  100. pUrl,
  101. pOndeInscreverResposta,
  102. pAsync=true
  103. ){
  104. var req = new XMLHttpRequest()
  105. if (req){
  106. req.open(
  107. "GET",
  108. pUrl,
  109. pAsync
  110. )
  111. req.onreadystatechange = acompanharPedido
  112. req.mOndeInscreverResposta = pOndeInscreverResposta
  113. req.send(null)
  114.  
  115. return true
  116. }
  117. else{
  118. return false
  119. }
  120. }//pedidoGet
  121.  
  122. function acompanharPedido(){
  123. var bPedidoEstaTotalmenteRespondido = this.readyState === 4
  124. &&
  125. this.status===200
  126.  
  127. if (bPedidoEstaTotalmenteRespondido){
  128. this.mOndeInscreverResposta.innerHTML = this.responseText
  129. }
  130. else{
  131. this.mOndeInscreverResposta.innerHTML+=`maturity:${this.readyState}<br>`
  132. }
  133. }//acompanharPedido
Add Comment
Please, Sign In to add comment