Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript http在cordova中不起作用_Javascript_Visual Studio_Cordova_Http Get - Fatal编程技术网

Javascript http在cordova中不起作用

Javascript http在cordova中不起作用,javascript,visual-studio,cordova,http-get,Javascript,Visual Studio,Cordova,Http Get,使用visual studio 2017、cordova并运行index.js脚本: function onDeviceReady(){ (...) document.getElementById("Button1").addEventListener("click", getfromAPI); function getfromAPI() { var theUrl = "https://haveibeenpwned.com/api/v2/breac

使用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 *">

您有两个问题:

  • CSP。您必须向其添加haveibeenpwned以允许连接
  • XHR代码不正确。您必须收听关于ReadyStateChange的
    onreadystatechange

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

  • 您是否安装了白名单插件?您是否有内容安全策略元标记?是的,白名单插件似乎是根据config.xml安装的,我还在说明中添加了内容安全策略标记