1

I have a PhoneGap project which runs perfectly fine on Android platform, but it doesn't run on WP8.

Aftering loading the index.html(the default page which is created when the project is created), I redirect the page to a new page called _layout.html.

Here is the index.js(it's the PhoneGap built-in code, not mine) which has my code for redirecting to my page.

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);
    }
};

Here is the code for _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>

As you can see that this page has a div which called "franva" and it loads another page, _resultlist.html, into this div.

Here is the code for _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>

I only created these 2 pages, nothing more than them.(oh also included jquery if that counts.)

I ran a cloud build on PhoneGap Build website to generate the Android app and it works on my Android phone. The online generated Windows Phone app cannot be installed(it pops up a error message : Cannot install this Company App ....)

So I built it in my Visual Studio 2012. But the content of div franva cannot be loaded.

I went through the PhoneGap document, it says CORS is not a problem in PhoneGap, since it has a WebBrowser underlying which runs code. It's true one Android, but why not on Windows Phone 8?

The whole idea is to have a layout page so that I don't need to write the duplicated layout part code again and again. The _resultlist.html page serves as a content of a div, it can be replaced by any other resources like the data fetched by an Ajax call.

Also, I have done a research about what the version of IE is used in WP 8, the answer is IE 10. About IE10, someone says it supports CORS, others say no....I am confused....

I have been stuck on this problem for days.

Really appreciate if someone can point out the right way for me.

Thanks in advance.

4

1 に答える 1

0

私は私の質問に対する解決策を得ました。

以下のコードを 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>

ただし、これは、情報を取得したい Web サイトにアクセスできる場合にのみ適用されます。持っていない場合は、それを行う独自の C# コードを作成し、それをモバイル アプリが使用する WebAPI として公開します。

同じ悩みを持つ方の参考になれば幸いです。

于 2014-01-08T12:53:00.060 に答える