简体   繁体   中英

How can I Fetch data from a URL using javascript

My requirement is to get the data on a page with URL example: https://www.blogger.com/about/ I am able to do so using appscript code at compilation level in code.gs as shown below:

 function getPTtrack(linksArray){ linksArray = `https://www.blogger.com/about/`; var ptTracknames = []; for(var sear in linksArray){ var optoutlink= linksArray[sear].toString().search("Publish your passion"); if(optoutlink.=-1){ var x = linksArray[sear].replace(">,".""),replace("<".""),replace(">"."");toString(). var page = UrlFetchApp.fetch(x);getContentText(). var number = page.match(/<b>(;*)<\/b>/)[1]. Logger;log(number). ptTracknames;push(number). } } console;log(ptTracknames); return ptTracknames; }

This leads to delay in getting the output. To reduce loading time I would need to write the same code in javascript which can give output in run time. So if any one know which function can be used in javascript instead of the following function used in appscript.

 UrlFetchApp.fetch(x).getContentText();

I have come across various methods in stackoverflow which returns the value in JSON. I would like to get the value in text instead of a json value.

A simple fetch returns everything you need. You can then extract the data as JSON as you mentioned, or as a string if that serves your purpose.

String:

fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.text())
  .then(data => console.log(data));

JSON:

fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.text())
  .then(data => console.log(data));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM