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?

1 Answer 1

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);
});

Sign up to request clarification or add additional context in comments.

1 Comment

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: javascript.info but let me know if you know about other nice resources for self-learning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.