skip to Main Content

Based on help obtained from@rkosegi , I tried to run my page and javascript through js.fiddle to test and cannot get any result to show on the test page. I get no errors when I run the page inspect in MS Edge.

I checked and double checked, but cannot find the error that is preventing the data from showing. What I am I missing?

$.getJSON("https://<username>:<password>@api.meteomatics.com/todayT05:00Z--today+2DT05:00Z:PT24H/wind_speed_10m:kmh/40.014994,-73.811646/json",

  function(data) {
    console.log(data);

    var mtwnsd24 = data.data[0].coordinates[0].dates[1].value;

    $(".mtwnsd24").append(mtwnsd24);
  });
<head>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge" />

  <meta charset="utf-8" />
  <title>API TEST</title>
  <script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256- 
      WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>

  <script src="meteomatics.js"></script>


</head>

<body>

  <p class="mtwnsd24"></p>

</body>

2

Answers


  1. Running your snipplet, checking requests: meteomatics.js not found, 404, and fails silently… so maybe it’s a remote script and you should complete the url?

    <script src="[**COMPLETE URL HERE**]meteomatics.js"></script>
    

    Related to the request itself, maybe you should use JSONP requests, because if no CORS headers are set, then it’s the logical alternative. You can enable JSONP adding to the url a callback parameter (check JSONP at https://api.jquery.com/jQuery.getJSON/ )

    Login or Signup to reply.
  2. The API works fine however you can’t pass authentication through the URL like you are trying to do, instead set the auth via headers. Using fetch for example will look like:

    var headers = new Headers();
    
    headers.append('Authorization', 'Basic ' + btoa(username + ':' + password));
    fetch("https://api.meteomatics.com/todayT05:00Z--today+2DT05:00Z:PT24H/wind_speed_10m:kmh/40.014994,-73.811646/json", {headers: headers})
    

    As others have mentioned there is a CORS issue so you will need to request the API from a server/proxy. I tried via Postman and successfully got a response from the API after just signing up.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search