Error "content-type is not allowed by Access-Control-Allow-Headers in preflight Response." -API Climatempo

Asked

Viewed 141 times

0

Hello, I have a problem with CORS in the Climatempo API, my request code is this:

var api_url = '"http://apiadvisor.climatempo.com.br/api/v1/anl/synoptic/locale/BR?token=TOKEN"';   
$.ajax({
    url: api_url,
    contentType: "application/json",
    crossDomain: true,
    dataType: 'json',
    success: function(result){
        console.log(result)
    }
})

I’ve tried switching to jsonp, but also I did not succeed, I’m stuck for a long time here and everything I saw on the internet did not solve

1 answer

1


They don’t accept the contentType: "application/json", in the configuration of Cors, in fact it is not necessary, because the contentType does not say what you expect from reply, it only states that you are sending JSON, but your request is GET and soon does not send anything, so that contentType nor should exist. Change to:

var api_url = '"http://apiadvisor.climatempo.com.br/api/v1/anl/synoptic/locale/BR?token=TOKEN"';   
$.ajax({
    url: api_url,
    crossDomain: true,
    dataType: 'json',
    success: function(result){
        console.log(result)
    }
})
  • Thank you for the answer ! Now I have another error Cross origin requests are only supported for Protocol schemes: http, data, Chrome, Chrome-Extension, https.

  • You have to run on an http page, as localhost @Gabrielnunes on FILE protocol does not work.

  • Thank you very much!!! It worked now!!

  • @Gabrielnunes for nothing, if possible mark the answer as correct

Browser other questions tagged

You are not signed in. Login or sign up in order to post.