Javascript fetch not working as expected | XM Community
Skip to main content

Question for the community, I have some working js code that loads a webservice URL from Google. Before you say "use the webservice built-in", that won't work as the response from google api's typically includes anti-spoofing code, so it isn't strict JSON without javascript manipulation. Here's the working code:

const email = "XXXXXX@xxx";
const netid = email.split('@')[0];
let myHeaders = new Headers();
myHeaders.append("X-DataSource-Auth", "true");
myHeaders.get("X-DataSource-Auth")
const myInit = {
 method: 'GET',
  headers: {
  // the content type header value is usually auto-set
  // depending on the request body
  "X-DataSource-Auth": "true"
 },
 mode: 'same-origin',
 cache: 'default',
};

//console.log(myInit)

const sheetID = 'XXXXXXX';
const base = `https://docs.google.com/spreadsheets/d/${sheetID}/gviz/tq?`;
const sheetName = 'Template';
let qu = 'Select * WHERE D CONTAINS \\'' + netid + '\\'';
const query = encodeURIComponent(qu);
const url = `${base}&sheet=${sheetName}&tq=${query}`;
const data = o];

fetch(url)
 .then(response => response.text())
 .then(rep => {
      //console.log(rep);
      const jsData = JSON.parse(rep.substr(47).slice(0, -2));
      console.log(jsData.version);
});

As a note, JQuery ajax fails, with no apparent reason as to why.

Be the first to reply!

Leave a Reply