不使用服务器端语言(也许是javascript?)来获取状态代码

发布于 2024-07-21 20:26:08 字数 345 浏览 3 评论 0原文

我创建了一项服务,通过复制/粘贴到错误页面模板中的代码片段(类似于 Google Analytics)来帮助处理网络应用程序中的错误页面。 状态代码设置为安装代码中的隐藏输入。 问题是我不希望安装代码依赖于服务器端语言(例如 PHP、Ruby) - 但应用程序确实可以使用动态方法来收集 http 请求中的状态代码,而无需硬编码的状态代码,因此需要为每个错误页面提供单独的安装代码(尽管我们实际上只关注 500 和 404;但就我而言,一个选项太多了)。

询问 Google 和我的开发朋友表明,通过 javascript 获取状态代码是不可能的(尽管我们以这种方式获取 http_referer),但我想知道是否有人提出了我尚未遇到的建议。 ..

I've created a service that helps with error pages in web apps via an snippet of code that is copy/pasted in to the error page templates (a la Google Analytics). The status code is set to a hidden input within the installation code. The problem is that I don't want the installation code to be dependent on a server-side language (e.g. PHP, Ruby) - but the app could really, really use a dynamic method of collecting the status code in the http request without having hard coded status codes and, therefore, a necessity for separate installation codes for each error page (although, we're really only looking at 500s & 404s; but one option is too many, as far as I'm concerned).

Quizzing Google and my dev friends suggests that getting the status code via javascript isn't going to be possible (we're getting the http_referer that way, though) but I wondered if anyone had any suggestions that I haven't come across yet...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

似梦非梦 2024-07-28 20:26:08

无法从 HTML 页面内或 JavaScript 函数访问 HTTP 状态代码。XMLHttpRequest 可以访问它,因为它知道自己创建的 Http 请求。 它首先是 HTTP 连接的发起者。 但是您要运行的 JavaScript 代码包含在已加载的页面中。

顺便说一句,这无论如何都会混合层(例如传输层和应用层)。 例如,由于您可以从文件系统加载 .html 页面 - 在这种情况下,HTTP 状态代码是什么? 或者,如果您从 FTP 服务器或 SVN 存储库访问 HTML 页面,则 HTTP 状态代码是什么?

There is no way of accessing the HTTP status code from within a HTML page or from a JavaScript function. XMLHttpRequest can access it, because it is aware of the Http request it has created on its own. It is the originator of the HTTP connection in the first place. But the JavaScript code you want to run is contained in a page which has already been loaded.

Btw, this would mix layers (e.g. transport layer and application layer) anyway. For example, since you can load a .html page from filesystem - what is the HTTP status code in this case? Or what is the HTTP status code if you're accessing a HTML page from an FTP server or from a SVN Repository?

安穩 2024-07-28 20:26:08

使用 XmlHttpRequest,您只需检索服务器上的文本/纯文件的内容,该文件将独立于您正在使用的服务器语言。

这可能是一个文本文件,其中的值以逗号或分号分隔,请求完成后您将对其进行拆分。

注意:您必须使用 XmlHttpRequest 的 responseText 属性。

希望我说得足够清楚。

With XmlHttpRequest, you can just retrieve the contents of a text/plain file located on your server that will be independant from the server language you're using.

This could be a text file with values separated by commas or semicolons that you'll split once the request is done.

Note : You'll have to use the responseText property of the XmlHttpRequest.

Hope I'm clear enough.

下壹個目標 2024-07-28 20:26:08

据我所知,无法使用嵌入在页面上的代码来找出页面本身的状态代码。 (对 URL 的 Ajax 请求将返回状态代码,因此理论上您可以对您所在的页面发出 Ajax 请求(即请求两次),但如果您需要在每次页面加载时都执行此操作,那就有点古怪了...)

我认为唯一的解决方案是按照您的建议进行操作并修改服务器端 404、500 等处理程序以在页面本身中嵌入有关状态代码的信息(例如 ),然后您可以使用 JavaScript 提取它。

As far as I know, there's no way to figure out the status code of a page itself using code embedded on that page. (An Ajax request to a URL will return the status code, so theoretically you could to an Ajax request to the page you're on (i.e request it twice), but that's a bit wacky if you need to do it on every page load...)

I think the only solution is to do as you suggest and modify your server-side 404, 500, etc. handlers to embed information about the status code within the page itself (e.g. <script>document.status = "404";</script>), which you can then extract with JavaScript.

乱世争霸 2024-07-28 20:26:08

尼尔.

这里有一些代码需要澄清。

function createXhrObject()
{
    if (window.XMLHttpRequest)
        return new XMLHttpRequest();

    if (window.ActiveXObject)
    {
        var names = [
            "Msxml2.XMLHTTP.6.0",
            "Msxml2.XMLHTTP.3.0",
            "Msxml2.XMLHTTP",
            "Microsoft.XMLHTTP"
        ];
        for(var i in names)
        {
            try{ return new ActiveXObject(names[i]); }
            catch(e){}
        }
    }
    return null; // not supported
}
xhr = createXhrObject();

xhr.onreadystatechange = function() 
{
    if(xhr.readyState == 4 && xhr.status == 200) 
    {
        document.getElementById('your_hidden_input').value = xhr.responseText;
    } 
}

xhr.open('GET', 'your_text_file.txt', true); 
xhr.send(null); 

Neil.

Here's some code to clarify.

function createXhrObject()
{
    if (window.XMLHttpRequest)
        return new XMLHttpRequest();

    if (window.ActiveXObject)
    {
        var names = [
            "Msxml2.XMLHTTP.6.0",
            "Msxml2.XMLHTTP.3.0",
            "Msxml2.XMLHTTP",
            "Microsoft.XMLHTTP"
        ];
        for(var i in names)
        {
            try{ return new ActiveXObject(names[i]); }
            catch(e){}
        }
    }
    return null; // not supported
}
xhr = createXhrObject();

xhr.onreadystatechange = function() 
{
    if(xhr.readyState == 4 && xhr.status == 200) 
    {
        document.getElementById('your_hidden_input').value = xhr.responseText;
    } 
}

xhr.open('GET', 'your_text_file.txt', true); 
xhr.send(null); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文