【问题标题】:Ajax header basic auth: request timeout not working on wp8Ajax 标头基本身份验证:请求超时不适用于 wp8
【发布时间】:2013-10-24 23:38:10
【问题描述】:

我目前正在为 VLC 媒体播放器编写遥控器。我使用 http-webinterface 连接和控制服务器。由于版本 2.1.0 VLC 需要设置密码。这本身不是问题。我用下面的 Ajax-Request 解决了它

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 试图显示弹出窗口,但失败了。那么,超时应该开始吗?

你们中有人遇到过同样的问题吗?如果有,你们是如何解决的?

【问题讨论】:

  • 好的。我试图调试它,但应用程序只是崩溃并且不会做其他事情。它根本不会反应。所以我仍在努力,希望有人能帮助我——不敢相信我是唯一一个遇到这样问题的人:)
  • 尝试使用“抛出异常时中断”选项运行应用程序 - VS 中的 Ctrl+Alt+E
  • 您好,感谢您的评论。但这不会改变任何事情。该应用程序仍然挂起:(而且我没有看到任何其他信息。

标签: jquery ajax cordova windows-phone-8


【解决方案1】:

终于解决了。这很简单,只需要用 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
    }
}
}

这是您将从 JavaScript 调用的位:

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

【讨论】:

    猜你喜欢
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    相关资源
    最近更新 更多