HTTP GET request in JavaScript

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

HTTP GET request in JavaScript

Post by Mr.Dandy »

Hello folks

I need to do this in Javascript.
All examples what I found are to use XMLHttpRequest object.
But which package for ImportClass should I input?

Code: Select all

importClass(Packages.com.sun.star.uno.UnoRuntime);
importClass(Packages.com.sun.star.text.XTextDocument);
importClass(Packages.com.sun.star.text.XText);
importClass(Packages.com.sun.star.text.XTextRange);
importClass(Packages.com.sun.star.frame.XModel);
// ImportClass( ??? XMLHttpRequest);

var req = new XMLHttpRequest();
req.open('GET', 'https://forum.openoffice.org', false);
req.send(null);
if(req.status == 200)
  var html = req.responseText;

oDoc = UnoRuntime.queryInterface(XModel,XSCRIPTCONTEXT.getInvocationContext());
if ( !oDoc )
  oDoc = XSCRIPTCONTEXT.getDocument();
xTextDoc = UnoRuntime.queryInterface(XTextDocument,oDoc);
xText = xTextDoc.getText();
xTextRange = xText.getEnd();
xTextRange.setString( html );
Thanks
OpenOffice 4.1.12 - Windows 10
Cazer
Posts: 53
Joined: Mon May 15, 2023 11:55 am

Re: HTTP GET request in JavaScript

Post by Cazer »

It seems there's some confusion here. XMLHttpRequest is a built-in object in browsers, used for making HTTP requests in JavaScript. It doesn't need to be imported like Java packages.

Your XMLHttpRequest code looks just fine for a simple synchronous GET request. But, I'd recommend using the Fetch API or async/await with XMLHttpRequest, for a more modern, asynchronous approach.

Also, the Java import statements you've provided are related to Apache OpenOffice's UNO API. If you're trying to use these with JavaScript, you might be mixing up languages and environments. Please clarify your requirement if this doesn't help!
OpenOffice 4.1.14
OS
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: HTTP GET request in JavaScript

Post by Mr.Dandy »

I'm searching for a way to fetch JSON data from a website.
I find HttpUrlConnection but the sample is in Java: viewtopic.php?t=71712
I don't know how to translate this code
OpenOffice 4.1.12 - Windows 10
User avatar
robleyd
Moderator
Posts: 5089
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: HTTP GET request in JavaScript

Post by robleyd »

Maybe these links can help - they refer to the Fetch API ?

How To Use the JavaScript Fetch API to Get Data
Get JSON with the JavaScript fetch method

Also see if any of the results from this web search may help.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: HTTP GET request in JavaScript

Post by Mr.Dandy »

robleyd wrote: Tue Jun 20, 2023 8:56 am Maybe these links can help - they refer to the Fetch API ?
Did you try to execute in Rhino editor?
These don't work.
OpenOffice 4.1.12 - Windows 10
User avatar
robleyd
Moderator
Posts: 5089
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: HTTP GET request in JavaScript

Post by robleyd »

Did you try to execute in Rhino editor?
No - why would I be expected to do that?

I only posted them as possible help for your question; I leave the decision as to whether they suit your needs to you. You made no previous reference to Rhino - whatever that may be.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: HTTP GET request in JavaScript

Post by Mr.Dandy »

robleyd wrote: Tue Jun 20, 2023 10:15 am You made no previous reference to Rhino - whatever that may be.
https://wiki.openoffice.org/wiki/Docume ... ipt_Editor
I don't know other editor to make this in AOO.
OpenOffice 4.1.12 - Windows 10
Post Reply