簡體   English   中英

Windows Phone 8.1的Cordova應用程序中的AJAX響應混亂

[英]AJAX responses messed up in Cordova application on Windows Phone 8.1

我們正在使用Cordova 3.4.0開發應用程序。 在Android和iOS上,以及在桌面瀏覽器中啟動應用程序,一切都可以正常運行。 但是我們在Windows Phone 8.1上遇到了一個非常奇怪的問題。

這是要測試的簡化代碼示例。

應用程序根目錄中的index.html:

<!DOCTYPE html>
<html>
    <head>
        <title>Mobile sandbox</title>
        <meta charset="UTF-8">
        <script type="text/javascript" src="libs/jquery/jquery.min.js"></script>
    </head>
    <body>

        <div id="redbox" style="width:100px;height:100px;background-color:red;">
        </div>

        <div id="greenbox" style="width:100px;height:100px;background-color:green;">
        </div>

        <script>
            $(function () {

                alert("Requesting data for redbox...");

                $.ajax("test/redText.html")
                  .done(function (text) {
                      alert("Filling redbox with contents " + text);
                      $("#redbox").html(text);
                  })
                  .fail(function () {
                      alert("Error in redbox");
                  })
                  .always(function () {
                      alert("Complete redbox");
                  });

                alert("Requesting data for greenbox...");

                $.ajax("test/greenText.html")
                  .done(function (text) {
                      alert("Filling greenbox with contents " + text);
                      $("#greenbox").html(text);
                  })
                  .fail(function () {
                      alert("Error in greenbox");
                  })
                  .always(function () {
                      alert("Complete greenbox");
                  });

            });
        </script>
    </body>
</html>

測試/ greenText.html:

<span>GREEN</span>

測試/ redText.html:

<span>RED</span>

運行此測試的唯一依賴項是jQuery,我們已將其放入libs / jquery /文件夾中。

現在,如果我們在每個桌面瀏覽器以及iOS和Android中運行此代碼,無論是從本地文件夾(帶有本地AJAX的瀏覽器標志)還是從服務器運行,我們都將獲得正確的警報順序,並且AJAX會在適當的框中加載正確的數據:

Requesting data for redbox...
Requesting data for greenbox...
Filling redbox with contents <span>RED</span>
Complete redbox
Filling greenbox with contents <span>GREEN</span>
Complete greenbox

如果通過Windows Phone的Internet Explorer在index.html上運行index.html,則會得到相同的結果。

但是,當我們將與Cordova應用程序相同的代碼部署到Windows Phone時,會發生奇怪的事情。 redbox請求永遠不會收到任何數據,也不會收到任何錯誤。 greenbox請求接收的數據redbox ,因此,我們在里面有空紅色框和綠框帶有文本“紅色”。 以下是警報的順序:

Requesting data for redbox...
Requesting data for greenbox...
Filling greenbox with contents <span>RED</span>
Complete greenbox

那里發生了什么,為什么一個AJAX請求不返回而另一個收到錯誤的響應? 我們該如何解決?

編輯1:

我們的嵌套步驟將是查找是否是Cordova特定的問題(我看到Corodva WP8模板中有一些XHRHelper對象)還是Microsoft的phone:WebBrowser錯誤。

編輯2:

似乎,WebBrowser本身不支持對本地文件的AJAX請求(我得到“拒絕訪問”),這就是Cordova發明XHRHelper類的原因。 但是我發現了一個相關的錯誤報告,他們將其關閉為“無法復制”: https : //issues.apache.org/jira/browse/CB-4873

這里有沒有任何Cordova開發人員可以建議XHRHelper的修復程序,因此它支持多個順序的AJAX請求?

同樣,我無法復制您的結果。 我很快整理了一個版本,將其發布到JIRA問題中: https : //issues.apache.org/jira/browse/CB-4873

Requesting data for redbox...
Requesting data for greenbox...
Filling redbox with contents 
<span>REd</span>
Complete redbox
Filling greenbox with contents 
<span>GREEN</span>
Complete greenbox

我修改了您的源代碼只是為了確保沒有警報干擾問題...

var eventLog = [];
var oneDone = false;

$(function () {
    eventLog.push("Requesting data for redbox...");
    $.ajax("redText.html")
        .done(function (text) {
            eventLog.push("Filling redbox with contents " + text);
            $("#redbox").html(text);
        })
        .fail(function (e) {
            eventLog.push("Error in redbox" + JSON.stringify(e));
        })
        .always(function () {
            eventLog.push("Complete redbox");
            if (oneDone) {
                console.log(eventLog.join("\n"));
                alert(eventLog.join("\n"));
            }
            else {
                oneDone = true;
            }
        });

    eventLog.push("Requesting data for greenbox...");
    $.ajax("greenText.html")
        .done(function (text) {
            eventLog.push("Filling greenbox with contents " + text);
            $("#greenbox").html(text);
        })
        .fail(function () {
            eventLog.push("Error in greenbox");
        })
        .always(function () {
            eventLog.push("Complete greenbox");
            if (oneDone) {
                console.log(eventLog.join("\n"));
                alert(eventLog.join("\n"));
            }
            else {
                oneDone = true;
            }
        });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM