0

我在将 phonegap 应用程序从 Android 和 iOS 移动到 WP8 时遇到问题。当我尝试加载一些语言 .JSON 文件时,它似乎崩溃了。我使用的版本是 phonegap 2.9.0 和 jQuery 2.0.3。一切都在 Android 和 iOS 上按预期工作。

控制台输出:

'TaskHost.exe' (CLR C:\windows\system32\coreclr.dll: Silverlight AppDomain): Loaded 'C:\windows\system32\System.Runtime.Serialization.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Updating IsolatedStorage for APP:DeviceID :: ********-****-****-****-***********
CordovaBrowser_Navigated :: www/index.html
CommandString : Device/getDeviceInfo/Device899915039/[]
'TaskHost.exe' (CLR C:\windows\system32\coreclr.dll: Silverlight AppDomain): Loaded         'C:\windows\system32\System.ServiceModel.Web.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TaskHost.exe' (CLR C:\windows\system32\coreclr.dll: Silverlight AppDomain): Loaded 'C:\windows\system32\en-US\mscorlib.debug.resources.dll'. Module was built without symbols.
CommandString : NetworkStatus/getConnectionInfo/NetworkStatus899915040/[]
An exception of type 'System.NotSupportedException' occurred in Microsoft.Phone.ni.dll and wasn't handled before a managed/native boundary
The thread 0x1160 has exited with code 259 (0x103).
The thread 0x80c has exited with code 259 (0x103).
CommandString : DebugConsole/log/DebugConsole899915041/"Received Event: deviceready"
Log:["Received Event: deviceready","DebugConsole899915041"]
The thread 0x8c8 has exited with code 259 (0x103).
CommandString : File/readResourceAsText/File899915042/["localization/nb-NO.json"]
A first chance exception of type 'System.IndexOutOfRangeException' occurred in no.visma.patentstyret.DLL
An exception of type 'System.IndexOutOfRangeException' occurred in no.visma.patentstyret.DLL but was not handled in user code

这是语言文件的ajax加载:

 var _loadDataSet = function(callback) {
        $.ajax({url: "localization/" + _language + ".json", async: false, dataType: 'json', success: function(data) {
            _dataSet = data;
            if(callback) {
                callback();
            }
        }}).error(function(e) {
            console.error("Error in language files.");
            console.error(e);
        });
    };

我不知道从哪里开始,任何帮助将不胜感激!

4

2 回答 2

1

请显示您对文件代码的阅读。

我正在使用 mustache 模板引擎制作应用程序,并且这样做了:

$.Mustache.load("./templates/about/about-app.tpl")

这没有加载,因为 WP8 需要完整路径:

$.Mustache.load("www/templates/about/about-app.tpl")

顺便说一句,WP7 - 不加载完整路径,只加载相对 =)))

还有一件事:

$.ajax({url: "www/localization/" + _language + ".json", async: false, dataType: 'json', success: function(data) {

WP 项目不喜欢 json 和其他扩展(有时它确实有效,有时不知道为什么),所以我建议你这样做:

1) 将文件类型更改为 *.txt

2) 要求:

$.ajax({url: "www/localization/" + _language + ".txt", async: false, dataType: 'text', success: function(data) {

更新:

差点忘了,要使用 AJAX,你必须这样做:

document.addEventListener('deviceready', function() {
    jQuery.support.cors = true;
    $.mobile.allowCrossDomainPages = true;
}, false);
于 2013-09-16T10:39:18.770 回答
0

解决方案是使用 XMLHttpRequest。在 Android、iPhone 和 Windows Phone 8 上测试并运行。虽然加载本地语言文件的解决方案是将数据添加到 JavaScript 类,但在 WP8 上使用 PhoneGap 的跨域请求仍然存在问题。

在WP8上实现跨域请求,我使用了XMLHttpRequests(也可以使用JSONP,不过这个也支持其他格式)。这是我最终使用的包装类:

(function() {
    name.of.package.HTTPRequest = function(destination, success, error, contentType) {
        var STATUS_IDLE = 0;
        var STATUS_OPEN = 1;
        var STATUS_LOADED = 2;
        var STATUS_WORKING = 3;
        var STATUS_DONE = 4;

        var req = new XMLHttpRequest();
        req.open('GET', destination, true);

        if(contentType) {
            req.setRequestHeader("Content-Type", contentType);
        }
        else {
            req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        }

        req.onreadystatechange = function(aEvt) {
            if(req.readyState == STATUS_DONE) {
                if(req.status == 200) {
                    if(success) {
                        success(req.responseText);
                    }
                }
                else {
                    if(error) {
                        error("Response returned with error code: " + req.status);
                    }
                }
            }
        };

        req.send(null);
    };
}) ();
于 2013-09-17T13:56:31.507 回答