Javascript从服务器获取纯文本 [英] Javascript get plain text from server

查看:112
本文介绍了Javascript从服务器获取纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的服务器收到一个纯文本(或任何文档)作为字符串变量,例如在alert中显示我试过这个解决方案

  wordsurl =http:// alpha / test; 
功能ButtonClicked(){
showsentence(wordsurl)
}

功能HTTPGET(theUrl)
{
如果(window.XMLHttpRequest)
{//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
}
else
{//代码为IE6,IE5
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange =函数()
{
如果(xmlhttp.readyState == 4安培;&安培; xmlhttp.status == 200)
$ { b $ b return xmlhttp.responseText;
}
}
xmlhttp.open(GET,theUrl,false);
xmlhttp.send(); // NS_ERROR_FAILURE在这里


函数showsentence(generatorurl){
alert(httpGet(generatorurl));
}

但是我得到NS_ERROR_FAILURE。这是指发送。
这里是一个



如何从服务器获取纯文本?



下面是服务器代码

解决方案

p>

你应该从 http:// 服务器提供你的html页面。不能作为文件 file://
所以设置一个简单的http服务器,并尝试再次访问它。您也可以从同一台服务器提供服务,例如您的应用逻辑


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

解决方案

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

这篇关于Javascript从服务器获取纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆