简体   繁体   English

如何在没有 CORB 警告的情况下检查 URL 是否存在?

[英]How can I check URL exists without CORB warning?

Some cities in our country are blocked from accessing some websites (for example Twitter).我们国家的一些城市被禁止访问某些网站(例如 Twitter)。

My website needs to hide some services for different users (for example hide login with Twitter for the user who can not access it).我的网站需要为不同的用户隐藏一些服务(例如,为无法访问它的用户隐藏使用 Twitter 的登录)。

I am about to check whether Twitter can be accessed in the front-end and hide the service or not.我正在检查Twitter是否可以在前端访问并隐藏服务。

Here is the code:这是代码:

function urlExists(url, callback) {
    $.ajax({
        type: 'HEAD',
        url: url,
        dataType:"jsonp",
        timeout: "1000",
        statusCode: {
            404: function () {
                alert("404");
            },
            200: function () {
                alert("200");
            }
        }
    });
}
var a = urlExists("https://twitter.com/");

Actually, it works well while in Chrome it throws a warning:实际上,它在 Chrome 中运行良好,它会引发警告:

Cross-Origin Read Blocking (CORB) blocked cross-origin response https://twitter.com/?callback=jQuery35100680029209969133_1611985917654&_=1611985917655 with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.

I don't want this warning to the user.我不希望向用户发出此警告。

I have found a way in How can I remove the CORB warning?我在如何删除 CORB 警告中找到了一种方法? while it seems have to use an API of Chrome.虽然它似乎必须使用 Chrome 的 API。 However, there is CROB also in Firefox, I don't want to write a code for each browser only for this.不过在Firefox中也有CROB,我不想只为此为每个浏览器写代码。

Is there any way to achieve this by js/jquery without this warning?有没有办法通过 js/jquery 实现这一点而没有这个警告? Thank you.谢谢你。

Append xhrFields option for your ajax 's options. Append xhrFields选项用于ajax的选项。

The XMLHttpRequest.withCredentials property is a Boolean that indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies, authorization headers or TLS client certificates. XMLHttpRequest.withCredentials 属性是一个 Boolean,它指示是否应使用诸如 cookies 或授权标头之类的凭据进行跨站点访问控制请求。 Setting withCredentials has no effect on same-site requests.设置 withCredentials 对同站点请求没有影响。

Ref link. 参考链接。

Your function will become like:你的 function 会变成这样:

function urlExists(url, callback) {
    $.ajax({
        type: 'HEAD',
        url: url,
        dataType:"jsonp",
        timeout: "1000",
        xhrFields: { // this option
            withCredentials: true,
        },
        statusCode: {
            404: function () {
                alert("404");
            },
            200: function () {
                alert("200");
            }
        }
    });
}
var a = urlExists("https://twitter.com/");

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

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