Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
How do i use an API on the P5js library?

I've been working on the p5js library and i'm trying to use an API to get access to weather data for my project, but the editor keeps throwing an error and I don't know why Here's my code:

function setup() {
  createCanvas(400,400); 
  let url = 'http://api.openweathermap.org/data/2.5/weather?q=London&appid=e7402cc176aacd446829a856f2723b57&units=metric'
  loadJSON(url,gotData)
}

function gotData(data){
  print(data);  
}

function draw(){
}

Can someone help point out what's wrong with my code?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should post the errors you're getting. (Since you were already using the p5.js editor you could also share a link so it's easier to for everyone else to replicate your issue)

I've just tried your code in the p5 web editor and I'm getting this error in the browser's JS Console:

p5.js:84555 Mixed Content: The page at 'https://editor.p5js.org/' was loaded over HTTPS, but requested an insecure resource 'http://api.openweathermap.org/data/2.5/weather?q=London&appid=e7402cc176aacd446829a856f2723b57&units=metric'. This request has been blocked; the content must be served over HTTPS.

Sometimes errors can be cryptic, but in this case it's pretty straight forward:

  • p5.js is loaded from an HTTPS domain (https://editor.p5js.org)
  • the weather API is accessed via HTTP(unsecure) (http://api.openweathermap.org/...etc.)
  • Ideally you'd load both via HTTPS (and the Weather API supports HTTPS).

This is makes the solution as simple as adding a single 's' :) (from http:// to https:// when accessing the Weather API)

function setup() {
  createCanvas(400,400); 
  let url = 'https://api.openweathermap.org/data/2.5/weather?q=London&appid=e7402cc176aacd446829a856f2723b57&units=metric'
  loadJSON(url,gotData)
}

function gotData(data){
  print(data);  
}

function draw(){
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda