簡體   English   中英

如何僅向真實域發送 XHR 請求?

[英]How to send XHR request only to real domains?

我正在發送XMLHttpRequest並且只想將它發送到真實域,這些域不為空並且不是 Chrome 的內部chrome:域。

直到現在我都失敗了 - 我的XMLHttpRequest在任何情況下都在發送 - 但在currentDomaon == 0currentProtocoll == "chrome:"情況下,它是空的。

我在做什么錯? 我應該如何調整代碼以達到目標?

var currentDomain = "";
var currentProtocol ="";
const processingTabId = {};

function run(tab) {
    if (processingTabId[tab.id]) return;
    processingTabId[tab.id] = true;

    let newUrl = new URL(tab.pendingUrl || tab.url)
    currentDomain = newUrl.hostname;
    currentProtocol = newUrl.protocol;

        var xhr = new XMLHttpRequest();
        var protocol = "https://";
        var middle = ".myservice/"
        var end = "/action/data/";

        xhr.open("GET", protocol + middle + currentDomain + end, true);

        xhr.responseType = 'document';

if (currentDomain !== null) && (currentProtocol !== "chrome:") {
xhr.send();
        }       
}

您可以使用正則表達式來確定字符串是否是以 HTTP/HTTPS 開頭的有效 URL。

例如:

https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)

請參閱堆棧溢出帖子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM