Server-side API returning XMLHttpRequest is not defined

Hello, I tried to query from the server-side using the suggested helper function below but I got XMLHttpRequest is not defined

// Initialize the Prismic api
// accessToken removed because of public API
function initApi(req){
  return Prismic.client('https://your-repo-name.cdn.prismic.io/api/v2', {
    req: req
  });
}

I'm using it on Astro components.
But when I use it on the client-side it's working.

Is it possible I'm missing something?

Hello @yver

Thanks for posting this question to us.

XMLHttpRequest is a built-in object in web browsers. Try to install it separately: (https://www.npmjs.com/package/xmlhttprequest),

  1. Install it with npm,
npm install xmlhttprequest
  1. Now you can require it in your code.
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();

Let me know if you still have any doubt.

Thanks,
Priyanka

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.