本地文件上的Windows Phone 8 jQuery.ajax失败

Windows Phone 8 jQuery.ajax on local file fails

本文关键字:jQuery ajax 失败 Phone Windows 文件      更新时间:2023-12-27

尽管我读了很多关于这个问题的讨论,但还是找不到一个有效的答案。

我正在开发一个phonegap应用程序,它应该在Windows Phone和iOS上运行。对于国际化,我使用jQuery globalize(https://github.com/jquery/globalize)它在桌面浏览器(IE、Firefox)和Safari中运行良好。然而,我需要访问一些JSON文件来初始化全球化引擎,这是使用Windows Phone时的问题,因为我找不到所需的文件。这些文件位于目录res/lang/cldr/中,该目录有两个子目录,其中包含特定于语言的文件。这是我加载所需文件的代码:

$.when(
    $.ajax("res/lang/cldr/likelySubtags.json", {
        cache: false, isLocal: true, async: false, dataType: "json", error: function (exceptionType, exceptionStatus, httpStatus) {
            // TODO: remove in production version
            console.log(httpStatus);
        }
    }),
    $.ajax("res/lang/cldr/timeData.json", { cache: false, isLocal: true, async: false, dataType: "json" }),
    $.ajax("res/lang/cldr/weekData.json", { cache: false, isLocal: true, async: false, dataType: "json" }),
    $.ajax("res/lang/cldr/en/numbers.json", { cache: false, isLocal: true, async: false, dataType: "json" }),
    $.ajax("res/lang/cldr/en/timeZoneNames.json", { cache: false, isLocal: true, async: false, dataType: "json" }),
    $.ajax("res/lang/cldr/en/caGregorian.json", { cache: false, isLocal: true, async: false, dataType: "json" }),
    $.ajax("res/lang/cldr/de/numbers.json", { cache: false, isLocal: true, async: false, dataType: "json" }),
    $.ajax("res/lang/cldr/de/timeZoneNames.json", { cache: false, isLocal: true, async: false, dataType: "json" }),
    $.ajax("res/lang/cldr/de/caGregorian.json", { cache: false, isLocal: true, async: false, dataType: "json" })
).then(function () {
    // Normalize $.ajax results, we only need the JSON, not the request statuses.
    return [].slice.apply(arguments, [0]).map(function (result) {
        return result[0];
    });
}).then(Globalize.load).then(function () {
    // load the messages displayed by the application
    $.ajax("res/lang/messages.json", {
        isLocal: true, async: false, dataType: "json", success: function (messages, status, request) {
            Globalize.loadMessages(messages);
        }, error: function (request, status, error) {
            alert(status);
        }
    });
});

我尝试了以下解决方案,但没有这些工作:

$.ajax("www/res/lang/cldr/likelySubtags.json", ...
$.ajax("/www/res/lang/cldr/likelySubtags.json", ...
$.ajax("/res/lang/cldr/likelySubtags.json", ...
$.ajax("./res/lang/cldr/likelySubtags.json", ...
$.ajax("./www/res/lang/cldr/likelySubtags.json", ...
$.ajax("x-wmapp0://www/res/lang/cldr/likelySubtags.json", ...
$.ajax("x-wmapp0:/www/res/lang/cldr/likelySubtags.json", ... (jsconsole.com used this as name of the file causing the exception
$.ajax("x-wmapp0://res/lang/cldr/likelySubtags.json", ...
$.ajax("x-wmapp0:/res/lang/cldr/likelySubtags.json", ...

然而,直接从根目录中加载文件效果良好:

// works
$.ajax({ url: fragmentURL, dataType: "html", cache: true, isLocal: true, processData: true, success: function(switchableFragment) { ... } }); // It works!!!

我感谢任何帮助!提前感谢!

Cordova for Windows Phone(我在版本3.5或更低版本上确认过)在加载JSON文件时遇到了一些问题。要解决此问题,您应该使用XHRHelper.cscordova文件,或者尝试更简单的方法。让我们看看这两种解决方案:

1-您可以在HandleCommand方法内的XHRHelper.cs上尝试以下代码:

Uri relUri = new Uri(uri.AbsolutePath, UriKind.Relative);
if (uri.AbsolutePath.EndsWith("json"))
{
    using (StreamReader r = new StreamReader(uri.AbsolutePath))
    {
        string json = r.ReadToEnd();
        InvokeCallback(url, 200, json);
        return true;
    }
}

2-最简单的方法。您可以尝试将JSON文件扩展名更改为TXT,然后使用Javascript将结果字符串解析为JSON对象。参见:

$.ajax({
    type: 'GET',
    dataType: 'text',
    url: 'res/lang/cldr/JSONFile.txt',
    success: function (data) {
        var content = JSON.parse(data);
        doWhateverYouWant(content);
    }
});

当我遇到同样的问题时,机器人的解决方案对我有效。希望它能有所帮助!

致以最良好的问候!