http在科尔多瓦不工作

http get not working in cordova

使用 visual studio 2017、cordova 和 运行 index.js 脚本:

    function onDeviceReady(){
    (...)
    document.getElementById("Button1").addEventListener("click", getfromAPI);
    function getfromAPI() {
        var theUrl = "https://haveibeenpwned.com/api/v2/breaches";
        var request = new XMLHttpRequest();
        var response = request.responseText;
        console.log(response);

        request.open("GET", theUrl, true);
        request.send(null);
    }

很遗憾,日志中没有收集任何内容,我不确定请求是否有效?

我尝试了一些命令的不同放置,但没有任何区别:

    function onDeviceReady(){
    (...)
    document.getElementById("Button1").addEventListener("click", getfromAPI);
    function getfromAPI() {
        var theUrl = "https://haveibeenpwned.com/api/v2/breaches";
        var request = new XMLHttpRequest();


        request.open("GET", theUrl, true);
        request.send(null);
        var response = request.responseText;
        console.log(response);

    }

内容安全策略如下:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: 
gap: https://ssl.gstatic.com 'unsafe-eval'; 
style-src 'self' 'unsafe-inline'; 
media-src *">

您有 2 个问题:

  1. CSP。您必须向其中添加 haveibeenpwned 才能允许连接 <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com https://haveibeenpwned.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
  2. XHR 代码不正确。您必须收听 onreadystatechange

    request.onreadystatechange = function() {
        if (request.readyState == 4 && request.status == 200) {        
            console.log(request.responseText);        
        }
    };