Jquery Ajax标头基本身份验证:请求超时无法在wp8上工作
我目前正在为VLC媒体播放器编写一个遥控器。我使用HTTPWebInterface连接并控制服务器。由于版本2.1.0,VLC需要设置密码。这本身不是问题。我通过以下Ajax请求解决了这个问题Jquery Ajax标头基本身份验证:请求超时无法在wp8上工作,jquery,ajax,cordova,windows-phone-8,Jquery,Ajax,Cordova,Windows Phone 8,我目前正在为VLC媒体播放器编写一个遥控器。我使用HTTPWebInterface连接并控制服务器。由于版本2.1.0,VLC需要设置密码。这本身不是问题。我通过以下Ajax请求解决了这个问题 checkConnection = function(id, folder){ $.ajax({ url: 'http://' + data.ip + ":" + data.port + '/requests/status.xml', headers: { "Authori
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
}
});
};
如果我使用我的计算机连接到VLC http接口,就会出现一个标准弹出窗口,询问用户名和密码。我现在的问题是,如果data.authorization中的令牌错误,应用程序(使用手机)就会崩溃。如果使用Ripple(使用Chrome)进行测试,则会显示所提到的弹出窗口,但超时有效,我的错误处理也会生效。这不是我的Windows Phone上的情况-这里我的应用程序挂起(如前所述)。我确实怀疑,因为它是一个webview WP试图显示弹出窗口,但失败了。再说一次,超时应该开始了
你们中有没有人有同样的问题?如果有,你是如何解决的?最终解决了。这很简单,只需要用C#编写一个插件。我将为可能遇到相同问题的任何人附上代码
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(string options)
{
//Parse data that gets passed into the plugin
string[] passedData = JSON.JsonHelper.Deserialize<string[]>(options);
string ip = passedData[0];
string port = passedData[1];
string username = passedData[2];
string password = passedData[3];
try
{
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
string credentials = String.Format("{0}:{1}", username, password);
byte[] bytes = Encoding.UTF8.GetBytes(credentials);
string base64 = Convert.ToBase64String(bytes);
string authorization = String.Concat("Basic ", base64);
webClient.Headers["Authorization"] = authorization;
string url = //your url here
var uri = new Uri(url);
webClient.DownloadStringAsync(uri);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Data);
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ""));
timeoutTimer.Stop();
}
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs 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
}
}
}
好吧我试图调试它,但应用程序只是崩溃,无法做其他事情。它根本不会反应。因此,我仍在努力,希望有人能帮助我-真不敢相信我是唯一一个有这样问题的人:)尝试使用“引发异常时中断”选项运行应用程序-Ctrl+Alt+E,谢谢你的评论。但这并没有改变任何事情。应用程序仍然挂起,我看不到任何其他信息。
cordova.exec(connectionSuccess, connectionError, "BasicAuth", "get", [data]);