在不使用服务器端语言的情况下抓取状态代码(也许是javascript?)

时间:2022-03-06 15:34:23

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).

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

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...

测验谷歌和我的开发朋友建议通过javascript获取状态代码是不可能的(虽然我们正在以这种方式获取http_referer)但我想知道是否有人有任何我尚未遇到的建议。 ..

4 个解决方案

#1


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.

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

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?

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

#2


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.

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

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.

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

Hope I'm clear enough.

希望我足够清楚。

#3


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...)

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

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.

我认为唯一的解决方案是按照您的建议并修改服务器端404,500等处理程序,以在页面内嵌入有关状态代码的信息(例如

#4


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); 

#1


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.

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

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?

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

#2


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.

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

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.

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

Hope I'm clear enough.

希望我足够清楚。

#3


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...)

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

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.

我认为唯一的解决方案是按照您的建议并修改服务器端404,500等处理程序,以在页面内嵌入有关状态代码的信息(例如

#4


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);