Cedric Reichenbach
Cedric Reichenbach

Reputation: 9319

No internet connection in PhoneGap app inside WP7 emulator

I'm developing a PhoneGap app and testing on Windows Phone. I'm working with jQuery, including AJAX operation to grab data from websites. This works well on desktop browsers (including IE9) and Windows Phone 8 devices.

However, when I try to run my app in a WP7 emulator, AJAX requests keep resulting in an error: "No Internet session has been established". I checked if I could load an iframe, which didn't work either. Based on that, I guess my app doesn't have any internet connection. Even more confusing: window.navigator.onLine is true on startup.

The emulator itself does have internet connection; I can use the built-in browser without any problems.

What could be the reason for this issue; i.e. what further debugging steps can I take?

Upvotes: 0

Views: 529

Answers (2)

Cedric Reichenbach
Cedric Reichenbach

Reputation: 9319

I checked the app's network connection by checking Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.IsNetworkAvailable on startup, which returned false. After some digging in the dirt, I finally found the problem:

In my WMAppManifest file, all capabilities except ID_CAP_WEBBROWSERCOMPONENT were disabled. Temporarily enabling all of them solved my issue (the relevant one for this issue is probably ID_CAP_NETWORKING).

Why did it work on Windows Phone 8?

My guess is that when running a WP7 app on a WP8 device, required capabilities are automatically detected, which would be strange, since the auto-detection of capabilities has been removed in the WP8 SDK. However, I couldn't find any documentation to clear things up.

Upvotes: 1

Developer
Developer

Reputation: 4341

I think that this is a problem some of those, which i have faced, try something like:

First of i found out that when using jQuery + Windows Phone+ PhoneGap

Before calling AJAX function you must wait to event „deviceready” to fire and add 2 code rows:

document.addEventListener('deviceready', function () {
    jQuery.support.cors = true;
    $.mobile.allowCrossDomainPages = true;
    Examplerequest(); //Your request
}, false);

To access project files you need to make AJAX request Function Examplerequest() {

Var url = "www/templates/about/example.tpl";

$.ajax({
    url: url, //path to resource
    error: function (jqXHR, textStatus, errorThrown) { 
        //function on error
        alert(url + " Error: " + errorThrown);
    },
    dataType: options.externalTemplateDataType
}).done(function () { 
    //function when request is done
});

}

url – must NOT be relative! // in WP8 but in WP7 it must be relative ( or other way, don;t remember ).

Upvotes: 1

Related Questions