【问题标题】:Intel XDK HTML5 App: http request always returns 0英特尔 XDK HTML5 应用程序:http 请求始终返回 0
【发布时间】:2016-06-09 09:19:12
【问题描述】:

我正在开发一个需要从服务器获取数据的 HTML5 应用程序。为此,我需要使用 Javascript,请参见下面的代码。但无论我做什么,它总是返回 0。它应该使用下面的代码返回一个 403 和正确的 url,或者 404 和错误的 url,它都不做。容器应用程序应允许跨站点脚本。我在 Intel App Preview 中运行了该应用程序。 Windows 10 / Android 5.1.1 Sony Experia Z1 Compact。

我想要它做的是:连接到服务器,给服务器正确的用户名;密码,返回一个数据包,用于应用程序。

测试了这些:
- Wifi 工作
- 与互联网连接一样
- 下面的 jQuery 版本。

    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://pageiuse.com", true);
    xhr.onload = function(){
    if (xhr.readyState == 4) {
        if(xhr.status == 200) {
            var json_string = xhr.responseText;
            var json = JSON.parse(json_string);
            toastMessage("Succes!");
        } else if(xhr.status == 404) {
            toastMessage("404!");
        } else {
            toastMessage("Error!!! " + xhr.status);
        }

    } else {
        toastMessage("readyState " + xhr.readyState);
    }};
    xhr.send(null);

欢迎所有帮助,或有关如何调试的建议。

*更新,当我使用 xhr.responseText 时有数据返回,但 xhr.status 保持为 0。一直在研究“Cordova 白名单”,但目前没有什么好转的。

【问题讨论】:

标签: javascript android html cordova intel-xdk


【解决方案1】:

我是这样解决的:

@xmnboy 提供了以下链接:use jQuery 2

这是我使用的代码:

$.ajax(
    {
    type: "GET",    
    crossDomain: true, //;paf; see http://stackoverflow.com/a/25109061/2914328
    url: "http://www.serverexample.com",
    async: true,
    dataType: "arraybuffer", // that is what I have incoming.
    beforeSend: function(xhr) {
        xhr.setRequestHeader("Authorization", "basic username:password");
        }
    })

    .always(function (retorno, textStatus, jqXHR) { //;paf; see http://stackoverflow.com/a/19498463/2914328
        //toastMessage("jQuery version: " + $.fn.jquery + "\n retorno:" + retorno.status + "\n textStatus:" + textStatus + "\n jqXHR:" + jqXHR.status) ;

    var returnStatus = jqXHR.status ;
    if (returnStatus === undefined) {
        returnStatus = retorno.status;
    }
    switch (returnStatus) {
            case 0:
                console.log("0 == exit oke", "Here") ;

            case 200:
                console.log("200 == exit oke", "Here") ;
                break;

            case 404:
                console.log("404 == exit by fail", "Here") ;
                break;

            default:
                console.log("default switch happened") ;
                console.log(JSON.stringify(jqXHR.responseJSON));
                break ;
        }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多