虽然,在阅读了大量关于这个问题的讨论后,我找不到一个有效的答案。
我正在开发一个phonegap应用程序,应该可以在Windows Phone和iOS上运行。为了国际化,我使用Safari (https://github.com/jquery/globalize),它在桌面浏览器(IE、Firefox)和jQuery中都能很好地工作。然而,我需要访问一些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!!!感谢大家的帮助!提前感谢!
发布于 2016-02-03 00:11:39
Cordova for Windows Phone (我在3.5版或更低版本中确认过)在加载JSON文件时有几个问题。要解决此问题,您应该使用XHRHelper.cs cordova文件或尝试一种更简单的方法。让我们来看看这两种解决方案:
1 -您可以在XHRHelper.cs inside HandleCommand方法中尝试以下代码:
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);
}
});当我遇到同样的问题时,机器人解决方案对我起作用了。希望它能帮上忙!
诚挚的问候!
https://stackoverflow.com/questions/28583747
复制相似问题