Phonegap和Windows Phone 7:IndexOutOfBoundsException

时间:2013-09-16 10:36:12

标签: windows-phone-8 cordova

我遇到了将手机应用程序从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);
        });
    };

我不知道从哪里开始,任何帮助都会非常感激!

2 个答案:

答案 0 :(得分:1)

请在文件代码中阅读。

我正在使用胡子模板引擎进行应用程序,并且这样做了:

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

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

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

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

还有一件事:

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

WP项目不喜欢像json和其他人这样的扩展(有时它确实有效,有时也不是没有IDEA为什么),所以我建议你这样做:

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);

答案 1 :(得分: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);
    };
}) ();