I am trying to get HTML pages using the API. Here is my code.
var quizUrl = 'http://www.lipsum.com/'; var myHeaders = new Headers(); myHeaders.append('Content-Type', 'text/html'); fetch(quizUrl,{ mode: 'no-cors', method: 'get', headers: myHeaders }).then(function(response) { response.text().then(function(text) { console.log(text); }) }).catch(function(err) { console.log(err) });
It returns an empty string. Any guesses why this is not working?
Oh (from MDN, emphasis mine)Request.mode 'no-cors'
Request.mode
'no-cors'
, HEAD, GET POST. - ServiceWorkers , , . , JavaScript - Response.. , ServiceWorkers , - .
, , Response opaque, , , , .
opaque
, , -.
PS: , , :
var quizUrl = 'http://www.lipsum.com/'; fetch(quizUrl, { mode: 'no-cors', method: 'get' }).then(function(response) { console.log(response.type) }).catch(function(err) { console.log(err) // this won't trigger because there is no actual error });
, , , :
fetch('/url/to/get/html/file') .then((res) => { return res.text(); }) .then((data) => { $('#container').html(data); }
$('#container') , html-, .
$('#container')
json- res.json() res.text()
res.json()
res.text()
Source: https://habr.com/ru/post/1014535/More articles:update element adding attribute to dynamodb - amazon-web-servicesDestroy resources created through Serverless without destroying Lambda endpoints - amazon-web-servicesBootstrap center aligns dropdown menu - htmlCSS Grid Layout: a grid of three rows - htmlTwilio Chat API, getUnconsumedMessagesCount is always 0 - javascriptAngular2 and Spring Boot. How to maintain the interface? - node.jsusage style binding for a one-time object returned as an option - f #vuejs v-for add boot line every 5 elements - javascriptPartial Gaussian Installation - numpyUsing the openpyxl module to write to a spreadsheet creates a damaged spreadsheet, how to fix it with a zipfile module? - pythonAll Articles