处理401未授权的错误在Windows Phone Phonegap [英] handle 401 unauthorized error on windows Phone with Phonegap
问题描述
我正在Windows phone 7上创建一个应用程序与phonegap,需要在服务器上的验证。当我们使用正确的用户名和密码登录时,我们获得了200状态。一切工作。但是,如果我们在登录或密码错误,服务器发送回401错误。这冻结了我的应用程序。我需要杀死它,并重新启动它。所以不是很实践。
I'm creating an application with phonegap on windows phone 7, that need an authentification on a server. When we log with the correct username and password, we got a 200 status. And everything works. But if we make a mistake on the login or the password, the server send us back a 401 error. This freeze my application. I need to kill it and restart it. So not very practice.
我在我的电脑上用fiddler检查响应,手机接收到401错误,我在我的代码上管理这个错误。这在我的其他平台(Android和ios)上工作。
I check the response with fiddler on my computer and the phone receive the 401 error, and I manage this error on my code. this works on my other platform (android and ios).
所以我想知道如何管理这个错误。也许我可以在windows phone项目上改变一个cs文件来处理这个错误。
So I would like to know how I can manage this error. Maybe I can change a cs file on the windows phone project to handle this error.
欢迎任何帮助
这是代码
$.support.cors = true;
$.ajax({
type: "POST",
dataType: "HTTP/1.1",
url: 'https://xxx.xxx-xxx.com/issue/wrap',
data: data,
cache: 'false',
async: false,
error: function (data) {
console.log(data);
console.log("error");
//navigator.notification.alert(data);
},
complete: saveToken
});
感谢
推荐答案
我遇到了同样的问题,我创建的唯一的解决方案是实现一个Phonegap插件。
I've encountered the same problem and the only solution I founded is to implement a Phonegap plugin.
这里是我使用的C#代码: p>
Here is the C# code I use :
namespace WPCordovaClassLib.Cordova.Commands
{
[DataContract]
public class PhonegapWindowsPhonePostObject
{
[DataMember(IsRequired = false, Name = "yourParamName1")]
public string yourParam1;
[DataMember(IsRequired = false, Name = "yourParamName2")]
public string yourParam2;
}
public class PhonegapWindowsPhonePost: BaseCommand
{
public void post(string options)
{
PhonegapWindowsPhonePostObject pwppo = JSON.JsonHelper.Deserialize<PhonegapWindowsPhonePostObject>(options);
try
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
var data = "YOUR DATA STRING HERE"; //use the pwppo object to retrieve your parameters
var url = "URL OF THE SERVICE HERE";
WebClient wc = new SharpGIS.GZipWebClient();
var URI = new Uri(url);
wc.Encoding = Encoding.UTF8;
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc__UploadStringCompleted);
wc.UploadStringAsync(URI, "POST", data);
});
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
void wc__UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
try
{
if (e.Result != null)
this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK, e.Result));
else
this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error 401"));
}
catch (Exception ex)
{
this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ex.Message));
// no http status code available
}
}
}
}
这里是从您的应用程序调用pluging的Javascript代码:
And here is the Javascript code to call the pluging from your app :
function loginToWebService(yourParam1, yourParam2) {
var options = { "yourParamName1": yourParam1, "yourParamName2": yourParam2};
cordova.exec(success, error,"PhonegapWindowsPhonePost","post", options);
}
我希望它会帮助你。
注意:Phonegap 2.8版本不能解决与发布说明相反的问题...
Note : the Phonegap 2.8 version does not solve the problem contrary to what said the release note...
再见!
这篇关于处理401未授权的错误在Windows Phone Phonegap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!