简体   繁体   English

W3C验证程序通过Ajax在localhost上脱机

[英]W3C Validator offline on localhost by ajax

I use to programming Brackets text editor and I have already installed W3C validator but it working while I'm online but offine not. 我曾经对Brackets文本编辑器进行编程,并且已经安装了W3C验证程序,但是当我在线时它可以正常工作,但不能正常工作。 I try install https://validator.w3.org/docs/install.html and I running to localhost:8888 but brackets's extension connecting only via ajax (javascript). 我尝试安装https://validator.w3.org/docs/install.html ,我运行到localhost:8888,但是方括号的扩展名仅通过ajax(javascript)连接。 Is possible send ajax like to original W3C website? 可以向原始W3C网站发送像ajax吗?

Maintainer of the W3C HTML checker (validator) here. W3C HTML检查器(验证器)的维护者。 Yes it is possible to send an ajax request to a local instance of the current checker. 是的,可以向当前检查器的本地实例发送一个ajax请求。 To use Fetch to do it and get JSON-formatted results back: 要使用Fetch进行操作并获取JSON格式的结果,请执行以下操作:

var checkerUrl = "http://localhost:8888/?out=json"
fetch(document.location.href)
.then(function(currentDoc) { return currentDoc.text(); })
.then(function(htmlSource) {
    fetch(
        checkerUrl, {
        method: "POST",
        mode: "cors",
        body: htmlSource,
        headers: new Headers({ "Content-Type": "text/html;charset=utf-8" })
    })
    .then(function(checkerResponse) { return checkerResponse.json(); })
    .then(function(jsonOutput) {
        console.dir(jsonOutput.messages);
    })
});

That shows the basic steps to follow to deliver the request in the way the checker expects: 这显示了按照检查员期望的方式交付请求所遵循的基本步骤:

  • send a document to the checker as the body of a POST (in this example, the current doc) 将文档作为POST正文发送到检查器(在此示例中,为当前文档)
  • tell the checker to format its results as JSON ( out=json ) 告诉检查器将其结果格式化为JSON( out=json
  • make text/html;charset=utf-8 the media type of the POST body you send to the checker 使text/html;charset=utf-8成为您发送给检查程序的POST正文的媒体类型

The checker also supports multipart/form-data for giving it the HTML source to be checked, but giving it the source as a POST body instead is the preferred (and better) way for doing it. 该检查器还支持multipart/form-data以向其提供要检查的HTML源,但是将其作为POST正文提供给它是首选(也是更好的方法)。

If instead of using fetch() you want to use JQuery $.ajax(…) , here's an example: 如果要使用JQuery $.ajax(…)而不是使用fetch() $.ajax(…) ,则下面是一个示例:

var checkerUrl = "http://localhost:8888/?out=json"
$.get(document.location.href,
function(htmlSource)
{
    $.ajax({
        url: checkerUrl,
        type: "POST",
        crossDomain: true,
        data: htmlSource,
        contentType: "text/html;charset=utf-8",
        dataType: "json",
        success: function (jsonOutput) {
            console.dir(jsonOutput.messages);
        }
    });
});

And if instead of either fetch() or JQuery $.ajax(…) you want to use old-school XHR but it's not clear how to handle the details in that case, let me know and I can post an example of that too. 而且,如果您想使用老式XHR而不是fetch()或JQuery $.ajax(…) ,但尚不清楚如何处理这种情况下的细节,请告诉我,我也可以发布一个示例。

In all cases, the .messages JSON output is an array of objects that each contain something like: 在所有情况下, .messages JSON输出都是一个对象数组,每个对象都包含以下内容:

firstColumn: 1
lastColumn: 6
lastLine: 4
message: "Unclosed element “span”."
type: "error"

The documentation for the checker JSON format gives more details of the JSON the checker emits. Checker JSON格式的文档提供了Checker发出的JSON的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM