1

I'm scratching a vanilla CesiumJS demo application from the 1.73 branch.

I left untouched the server.cjs file, but I added one custom client implementation file as Apps/myproject/my.js

As I need to GET some JSON responses from an existing API within that demo application, I tried to import the 'https' module as follows:

const https = require('https');

which conducted me to the Uncaught ReferenceError: require is not defined error.

Then I tried the import way:

import https;

which raised the Uncaught SyntaxError: missing keyword 'from' after import clause error.

(...)

I finally ended up with:

import * as https from 'https';

but => Uncaught TypeError: Error resolving module specifier “https”. Relative module specifiers must start with “./”, “../” or “/”. my.js:2:18

I'm running out of ideas for the moment.

How could I successfully and efficiently load and execute an http(s) request to actually get a JSON response from a given API within my custom Cesium app?

swiss_knight
  • 5,787
  • 8
  • 50
  • 92

1 Answers1

2

You can't use the "require" statement in the Cesiumjs client app.
Use this code snippet.

// replace it with your api url
const url = "https://www.google.com";

const promise = Cesium.Resource.fetchJson({
    url: url,
    queryParameters: {
    }
});

promise.then((response) =>{
  console.log(response);
}).otherwise(error =>{
  console.log(error);
});

ZhefengJin
  • 930
  • 8
  • 17
  • I like the simplicity of this answer, and it's fully working for JSON data. Thanks. I definitely need to update myself on "nowadays JS". I found this resource for that purpose: https://javascript.info/ but let me know if you know about other nice resources for self-learning. – swiss_knight Feb 12 '22 at 08:52