javascript - PhoneGap 中的跨域资源共享 (CROS) 似乎不适用于 Windows Phone 8

标签 javascript cordova windows-phone-8

我有一个 PhoneGap 项目,它在 Android 平台上运行得非常好,但它不能在 WP8 上运行。

加载index.html(创建项目时创建的默认页面)后,我将页面重定向到一个名为_layout.html的新页面。

这里是index.js(它是PhoneGap内置代码,不是我的),其中包含用于重定向到我的页面的代码。

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // `load`, `deviceready`, `offline`, and `online`.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of `this` is the event. In order to call the `receivedEvent`
    // function, we must explicity call `app.receivedEvent(...);`
    onDeviceReady: function() {
        app.receivedEvent('deviceready');


        // THIS IS THE ONLY CODE I WROTE IN THIS BUILT-IN JAVASCRIPT CODE
        window.setInterval(function () {
            window.location.href = '_layout.html';
        }, 3000);
       //---------------MY CODE ENDS--------------------------------------

    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};

这是_layout.html的代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <title>MyApp</title>

</head>

<body onload="loadPage('_resultlist.html');">
    <div class="panel">
        <div class="row" id="Title" style="text-align: center; vertical-align: central; position: relative; left: -6%; top: 10%; margin-bottom: -30px; margin-top: -20px;">
            <img src="img/logo.png" style="width: 150px; height: 100px; text-align: center; vertical-align: central;" />
        </div>
        <hr />
        <div id="franva" style="height: 300px; display:inline-block; width: 300px;">
        </div>
        <hr />
        <div id="search" style="text-align: center;">
            <input type="button" class="searchbutton" title="Search" value="Search" />
        </div>
    </div>
    <script type="text/javascript" src="js/jquery-2.0.3.js"></script>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript">
        function loadPage(url, onleave, onenter) {
            console.log("loadPage(" + url + ")");

            // If onleave function specified
            if (onleave) {
                onleave();
            }

            var xmlhttp = new XMLHttpRequest();

            // Callback function when XMLHttpRequest is ready
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState === 4) {
                    if (xmlhttp.status === 200) {
                        console.log("Received content" + xmlhttp.responseText);
                        $("#franva").html(xmlhttp.responseText);
                        // If onenter function specified
                        if (onenter) {
                            onenter();
                        }
                    }
                    else {
                        $("#franva").html("Error loading page " + url);
                    }
                }
            };
            xmlhttp.open("GET", url, true);
            xmlhttp.send();
        }

        jQuery.isUnsafe = true;

        function loadPageAjax(pageurl)
        {
            $.ajax({
                url: pageurl,
                context: document.body,
                dataType: "html"
            }).done(function (data) {
                alert("Ajax data = " + data);
                $("#franva").html(data);
            });
        }

        </script>
</body>

</html>

正如您所看到的,该页面有一个名为“franva”的 div,它会将另一个页面 _resultlist.html 加载到该 div 中。

这是 _resultlist.html 的代码

<div id="result-list" style="width: 100%;">
    <div class="result-row">
        <div class="left">
            <img src="img/tv1.jpg" />
        </div>
        <div class="right">
            <p><strong>Samsung XT7290</strong></p>
            <p>27 inch,  AU$ 1777</p>
        </div>
    </div>

    <div class="result-row">
        <div class="left">
            <img src="img/tv2.jpg" />
        </div>
        <div class="right">
            <p><strong>Samsung XT7290</strong></p>
            <p>27 inch,  AU$ 1777</p>
        </div>
    </div>

    <div class="result-row">
        <div class="left">
            <img src="img/tv3.jpg" />
        </div>
        <div class="right">
            <p><strong>Samsung XT7290</strong></p>
            <p>27 inch,  AU$ 1777</p>
        </div>
    </div>
</div>

我只创建了这两个页面,仅此而已。(哦,如果重要的话还包括jquery。)

我在 PhoneGap Build 网站上运行了云构建来生成 Android 应用程序,它可以在我的 Android 手机上运行。在线生成的Windows Phone应用程序无法安装(弹出错误消息:无法安装此公司应用程序....)

所以我在我的 Visual Studio 2012 中构建了它。但是 div franva 的内容无法加载。

我浏览了PhoneGap文档,它说CORS在PhoneGap中不是问题,因为它有一个运行代码的WebBrowser底层。 Android 确实如此,但为什么 Windows Phone 8 上不行呢?

整个想法是拥有一个布局页面,这样我就不需要一次又一次地编写重复的布局部分代码。 _resultlist.html 页面用作 div 的内容,它可以被任何其他资源替换,例如 Ajax 调用获取的数据。

另外,我还研究过WP 8使用的IE版本,答案是IE 10。 关于IE10,有人说支持CORS,有人说不支持……我很困惑……

我已经被这个问题困扰好几天了。

如果有人能为我指出正确的方法,我真的很感激。

提前致谢。

最佳答案

我的问题得到了解决方案。

只需在 Web.config 中添加以下代码

  <system.webServer>
   <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*"/>
            <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
        </customHeaders>
    </httpProtocol>
  </system.webServer>

但这仅适用于您有权访问要检索信息的网站。 如果没有,请编写自己的 C# 代码来执行此操作,并将其公开为 WebAPI 供您的移动应用使用。

希望对遇到同样问题的人有帮助。

关于javascript - PhoneGap 中的跨域资源共享 (CROS) 似乎不适用于 Windows Phone 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18973183/

相关文章:

ruby-on-rails - 使用 Phonegap 作为 Rails 3 应用程序的本地容器

windows-phone-8 - 为什么 XAML TextBox 中没有类似于 PlaceHolder 的属性

javascript - 在 React.js 中 data-reactid 破坏了 html 文本输入?

javascript - 将 javascript 三元运算符与 knockout 安全绑定(bind)一起使用时出错

javascript 和 ajax 加载,然后效果

javascript - href 不工作,如何重定向到 PhoneGap 中的特定页面?

javascript - 两个 Node.JS 脚本读取/写入单个 .JSON 文件,但第一个脚本无法修改该文件

ios - 为什么我在 cordova 中添加 ios 时得到 "command failed with exit code ENOENT"?

sqlite Windows Phone 8 应用程序示例

azure - 经过身份验证的推送通知,无需将应用程序发布到 Windows Phone 应用商店