且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将外部纯文本内容读入字符串

更新时间:2023-02-03 08:25:18

基于此 JavaScriper.net上的常见问题解答,我找到了适合我的解决方案。但是,被调用的脚本必须与调用者在同一台机器上,否则我会从浏览器中获得安全性错误。

Based on this FAQ on JavaScriper.net, I have found solution that works for me. However, the called script must be on the same machine as the caller, otherwise I get security errors from browsers.

显然这就是@Makkes提到的。但是,我现在非常高兴在同一台机器上安装hello.cgi。

Apparently this is what @Makkes mentioned. However, I'm perfectly happy with having the hello.cgi on the same machine for now.

以下是代码:

function loadThis(localuri) {
    var oRequest = new XMLHttpRequest();

    var sURL = 'http://'
        + self.location.hostname
        + localuri;

    oRequest.open('GET',sURL,false);
    oRequest.setRequestHeader('User-Agent',navigator.userAgent);
    oRequest.send(null);

    if (oRequest.status==200) return(oRequest.responseText);
    else alert('Error executing XMLHttpRequest call!');
}

name        = "Joe";
localuri    = "/hello.cgi?name=" + name;
greeting    = loadThis(localuri);

(当然,这不会正确处理带有空格或特殊字符的名称,但这是另一个故事。 )

(Of course, this would not handle names with spaces or special characters correctly, but that's another story.)