本地文件上的Windows Phone 8 jQuery.ajax失败
Windows Phone 8 jQuery.ajax on local file fails
尽管我读了很多关于这个问题的讨论,但还是找不到一个有效的答案。
我正在开发一个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);
}
});
当我遇到同样的问题时,机器人的解决方案对我有效。希望它能有所帮助!
致以最良好的问候!
相关文章:
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 将curl查询转换为jQuery.ajax()
- jQuery AJAX write to XML
- jQuery Ajax GET请求工作不正常
- jquery ajax https调用给出ERR_INSECURE_RESPONSE
- VB NET JQUERY AJAX上出现错误500
- 如何使用jquery ajax和jsonp在您自己的域上读取json数据
- Jquery/Ajax.serialize()未完全工作
- 页面加载之前的jQuery Ajax加载程序
- jquery/ajax无限滚动事件
- 使用Jquery$.ajax将json数据传递给servlet(doPost)
- 如何将给定的curl命令复制为jquery ajax请求
- 使用 JQUERY AJAX (Spring MVC) 从控制器中删除
- Twitter API在jQuery AJAX中设置授权头
- AngularJS JQuery Ajax表单提交等效
- jQuery Ajax.ajaxSuccess()事件未激发
- 为什么Jquery$.ajax在函数调用中触发所有statusCode,即使调用成功
- jQuery AJAX总是出错
- jQuery Ajax数组序列化错误
- jQuery AJAX获取Vimeo缩略图