Хотелось бы запустить live-сервер из скрипта на веб-странице localhost

Создайте веб-страницу на локальном диске (c: или F: и т. Д.)

скрипт на странице использует:

var xhttp = new XMLHttpRequest()

xhttp.open('GET', url, true)

xhttp.send()

Однако это создает ошибку:Access to XMLHttpRequest .... [web-URL] ...from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https

Решение - запустить live-server по тому же пути в командной строке.

Вопрос в том, может ли live-serverбыть вызванным из сценария. Например скрипт в<head> <\head> HTML-кода.

Я просмотрел методы sendkeys, но, похоже, это неправильный способ.

<html>
    <head>
            <title>Get Web Data</title>
            <script>
            // can the code fire up live-server from here ?
            </script>
    </head>

    <body>
        <!-- the body is here -->
        <a id='hello'> hello world </a><br>
        <b><a id='line02'> ... </a></b><br><br>
        <a id='line03'> ... </a><br>

        <!-- the code goes here -->
        <script>
            // a test hello
            console.log('hello')
            setInterval(()=>{ document.getElementById('hello').innerHTML = 'hello'}, 2000)
            setInterval(()=>{ document.getElementById('hello').innerHTML = 'darren did this'}, 4000)
            setTimeout(()=>{document.getElementById('line02').innerHTML = 'the output will go here'},3000)

            // get some data
            var xhttp = new XMLHttpRequest()
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    // Typical action to be performed when the document is ready:
                    document.getElementById("line03").innerHTML = xhttp.responseText;
                    console.log(xhttp.responseText)
                    var response = JSON.parse(xhttp.responseText)
                    }
            }
            // need to run live-server (but how??)
            url = 'www.bbc.co.uk'
            xhttp.open('GET', url, true)
            xhttp.send()
        </script>
    </body>

Хотелось бы автоматически запускать live-сервер, чтобы код запускался в браузере без необходимости выполнять то же самое из командной строки.

0 ответов

Другие вопросы по тегам