En-tête Ajax de base: le délai de requête ne fonctionne pas sur wp8

Je suis en train d’écrire une télécommande pour le lecteur multimédia VLC. J’utilise l’interface Web http pour me connecter et contrôler le serveur. Depuis la version 2.1.0, VLC nécessite la définition d’un mot de passe. Ce n’est pas un problème en soi. Je l’ai résolu avec la requête Ajax suivante

checkConnection = function(id, folder){ $.ajax({ url: 'http://' + data.ip + ":" + data.port + '/requests/status.xml', headers: { "Authorization" : "Basic " + data.authorization }, timeout: 3000, success: function (data, status, jqXHR) { //Yeah do stuff } }, error: function(data){ //Ohh, do stuff } }); }; 

Si je me connecte à l’interface http de VLC à l’aide de mon ordinateur, un message contextuel standard me demande le nom d’utilisateur et un mot de passe. Mon problème actuel est que, si le jeton dans data.authorization est erroné, l’application (à l’aide du téléphone) se bloque. Si testé avec Ripple (avec Chrome), la fenêtre contextuelle mentionnée apparaît, mais le délai d’attente fonctionne et mon traitement des erreurs s’installe. Ce n’est pas le cas sur mon Windows Phone – ici mon application se bloque (comme mentionné). Je soupçonne que, puisqu’il s’agit d’une vue Web, WP tente d’afficher la fenêtre contextuelle, mais échoue. Là encore, le timeout devrait commencer?

L’un de vous a-t-il eu le même problème et si oui, comment l’avez-vous résolu?

Enfin résolu. C’était assez facile et nécessitait simplement l’écriture d’un plugin en C #. Je joindrai le code à toute personne susceptible de rencontrer le même problème.

 using System; using System.IO; using System.Net; using System.Runtime.Serialization; using System.Text; using System.Windows.Threading; using WPCordovaClassLib.Cordova; namespace WPCordovaClassLib.Cordova.Commands { public class BasicAuth : BaseCommand { //Create timer to control timeout DispatcherTimer timeoutTimer = new DispatcherTimer(); WebClient webClient = new WebClient(); public BasicAuth(){ timeoutTimer.Interval = TimeSpan.FromSeconds(5); timeoutTimer.Tick += new EventHandler(timeout); timeoutTimer.Start(); } public void get(ssortingng options) { //Parse data that gets passed into the plugin ssortingng[] passedData = JSON.JsonHelper.Deserialize(options); ssortingng ip = passedData[0]; ssortingng port = passedData[1]; ssortingng username = passedData[2]; ssortingng password = passedData[3]; try { webClient.DownloadSsortingngCompleted += new DownloadSsortingngCompletedEventHandler(webClient_DownloadSsortingngCompleted); ssortingng credentials = Ssortingng.Format("{0}:{1}", username, password); byte[] bytes = Encoding.UTF8.GetBytes(credentials); ssortingng base64 = Convert.ToBase64Ssortingng(bytes); ssortingng authorization = Ssortingng.Concat("Basic ", base64); webClient.Headers["Authorization"] = authorization; ssortingng url = //your url here var uri = new Uri(url); webClient.DownloadSsortingngAsync(uri); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Data); DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "")); timeoutTimer.Stop(); } } void webClient_DownloadSsortingngCompleted(object sender, DownloadSsortingngCompletedEventArgs e) { try{ DispatchCommandResult(new PluginResult(PluginResult.Status.OK, e.Result)); //e.Result will fail if the server couldn't be contacted } catch{ DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "")); } } private void timeout(Object sender, EventArgs e) { webClient.CancelAsync(); //Cancel Async download timeoutTimer.Stop(); //Stop timer from beeing executed again } } } 

Voici le bit que vous appelez depuis votre JavaScript:

 cordova.exec(connectionSuccess, connectionError, "BasicAuth", "get", [data]);