Javascript get plain text from server

2019-07-08 10:06发布

问题:

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

How to get plain text from a server?

Here is Server code

回答1:

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



回答2:

  1. URL not found.(Is http://alpha/test in your computer?)

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