Javascript get plain text from server

耗尽温柔 提交于 2019-12-10 22:54:08

问题


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://



来源:https://stackoverflow.com/questions/33452181/javascript-get-plain-text-from-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!