Javascript get plain text from server

2019-07-08 10:17发布

I want to receive a plain text(or any document) from my server as a string variable and for example show it in alert I tried this solution

wordsurl = "http://alpha/test";
function ButtonClicked()    {
    showsentence(wordsurl)
}

function httpGet(theUrl)
{
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            return xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", theUrl, false );
    xmlhttp.send(); // NS_ERROR_FAILURE is here
}

function showsentence(generatorurl) {
    alert(httpGet(generatorurl));
}

but i'm getting NS_ERROR_FAILURE. Which is referring to the send. Here is a screenshot

How to get plain text from a server?

Here is Server code

2条回答
放我归山
2楼-- · 2019-07-08 10:21
  1. URL not found.(Is http://alpha/test in your computer?)

Maybe you can change file:// to http://

查看更多
Juvenile、少年°
3楼-- · 2019-07-08 10:43

Ah, now I see..

You should serve your html page from a http:// server.. Not as a file file:// So setup a simple http server, and try to access it again. You could also serve it from the same server, like your app logic

查看更多
登录 后发表回答