【发布时间】:2011-04-06 13:27:52
【问题描述】:
我有一个可供移动设备使用的 asp.net Web 应用程序。我使用 jQuery 和 jqMobile 来实现功能和样式。
该应用程序在 safari、google chrome、iPhone、iPad 和 Android 设备上运行良好,但除了黑莓手电筒之外,我无法让它在任何其他设备上运行。我需要让它在版本 5 和 6 黑莓设备上运行,但似乎登录的 ajax 请求总是调用错误函数,我不明白为什么。
该应用程序包含一些页面,但我什至无法通过黑莓上的登录页面。有没有其他人设法让 ajax 调用在黑莓上工作?我真的不想有一套单独的页面只是为了黑莓'
这是登录页面aspx的代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Sicon.Web.WAP.App.Pages.Mobile.Login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<link href="../../JavaScripts/jquery.mobile.min.css" rel="stylesheet" type="text/css" />
<script src="../../JavaScripts/jquery.min.js" type="text/javascript"></script>
<script src="../../JavaScripts/jquery.mobile.min.js" type="text/javascript"></script>
</head>
<body>
<form id="login" runat="server" accept-charset="utf-8">
<div id="Invoices" data-role="page" data-theme="b">
<div data-role="header" data-theme="b">
<h1>
WAP - Login</h1>
</div>
<div data-role="content" data-theme="b">
<div align="center">
<img src="Sicon_LogoHz_rgb72.png" />
</div>
<ul data-role="listview" data-inset="true">
<li>
<input type="text" value="" name="username" placeholder="Username" id="username" />
</li>
<li>
<input type="password" value="" name="password" placeholder="Password" id="password" />
</li>
</ul>
<a class="login" data-role="button" data-theme="b">Login</a> <a data-role="button"
data-theme="a">Cancel</a>
</div>
</div>
</form>
<script type="text/javascript">
var _ajaxEnabled = true;
$(document).ready(function()
{
_ajaxEnabled = $.support.ajax;
});
//Get base URL
var baseUrl = "<%= ResolveUrl("~/") %>";
//Function to resolve a URL
function ResolveUrl(url)
{
if (url.indexOf("~/") == 0)
{
url = baseUrl + url.substring(2);
}
return url;
}
//Login form Login link click
$("#login a.login").click(function (e) {
//Get the form
var $form = $(this).closest("form");
//Perform login
return app.login($form);
});
//Login form submit
$("#login").submit(function (e) {
//Get the form
var $form = $(this);
//Perform login
return app.login($form);
});
//class to handle login
var app = {
login: function ($form) {
var $Username = $("#username").val();
var $Password = $("#password").val();
//Call the approve method on the code behind
$.ajax({
type: "POST",
url: ResolveUrl("~/Pages/Mobile/Login.aspx/LoginUser"),
data: "{'Username':'" + $Username + "', 'Password':'" + $Password + "' }", //Pass the parameter names and values
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
error: function (jqXHR, textStatus, errorThrown) {
alert("Error- Status: " + textStatus + " jqXHR Status: " + jqXHR.status + " jqXHR Response Text:" + jqXHR.responseText) },
success: function (msg) {
if (msg.d == true) {
window.location.href = ResolveUrl("~/Pages/Mobile/Index.aspx");
}
else {
//show error
alert('login failed');
}
}
});
return false;
}
}
</script>
</body>
</html>
最后是登录方法背后的代码:
/// <summary>
/// Logs in the user
/// </summary>
/// <param name="Username">The username</param>
/// <param name="Password">The password</param>
/// <returns></returns>
[WebMethod, ScriptMethod]
public static bool LoginUser( string Username, string Password )
{
try
{
StaticStore.CurrentUser = new User( Username, Password );
//check the login details were correct
if ( StaticStore.CurrentUser.IsAuthentiacted )
{
//change the status to logged in
StaticStore.CurrentUser.LoginStatus = Objects.Enums.LoginStatus.LoggedIn;
//Store the user ID in the list of active users
( HttpContext.Current.Application[ SessionKeys.ActiveUsers ] as Dictionary<string, int> )[ HttpContext.Current.Session.SessionID ] = StaticStore.CurrentUser.UserID;
return true;
}
else
{
return false;
}
}
catch ( Exception ex )
{
return false;
}
}
【问题讨论】:
-
我已经尝试了这篇文章中列出的答案,检查了错误状态并返回。这只是意味着当错误被捕获时不会发生任何事情...stackoverflow.com/questions/1023867/jquery-xmlhttprequest-error
-
这一行有错字是否有原因:if (StaticStore.CurrentUser.IsAuthentiacted)
-
@JackWilson - 不,没有理由。虽然它是一个有效的属性名称,但它都可以编译。 WebMethod 的问题不在于其他设备可以调用它并登录。问题是黑莓总是调用 ajax 调用的错误处理程序,它在黑莓火炬上工作,因此代码可以工作,它只是不适用于其他版本 5 和 6 设备。
标签: asp.net jquery jquery-mobile blackberry-simulator