jQuery黑莓ajax问题

发布于 2024-10-31 01:53:11 字数 5629 浏览 2 评论 0原文

我有一个可供移动设备使用的 asp.net Web 应用程序。我使用 jQuery 和 jqMobile 来实现功能和样式。

该应用程序在 safari、google chrome、iPhone、iPad 和 Android 设备上运行良好,但除了 Blackberry torch 之外,我无法让它在任何其他设备上运行。我需要让它在版本 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;
    }
}

I have an asp.net web application that I am making available to mobile devices. Im using jQuery and jqMobile for the functionality and styling.

The application works great in safari, google chrome, on the iPhone, iPad, and Android devices but I cant get it working on anything other than the Blackberry torch. I have a requirement to get it working on version 5 and 6 blackberry devices but it seems the ajax request for logging in is always calling the error function and I cant see why.

enter image description here

The application contains a few pages but I cant even get past the login page on the blackberry. Has anyone else managed to get ajax calls working on the blackberry? I dont really want to have a seperate set of pages just for blackberrys'

Here is the code for the login page 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>

And finally the code behind for the login method:

/// <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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹉夏雨初晴づ 2024-11-07 01:53:11

你没有说你用的是哪个版本的jqmobile。 4.1 版本已于 4 月 7 日发布。您必须记住,jqmobile 甚至还没有 Beta 版本,并且 BB OS 5 被列为 B 级 (http://jquerymobile.com/gbs/),因此它正在获得较少关注。

您的应用程序中可能还有其他 ajax 需要处理,但登录如此简单,为什么不将其重组为简单的表单帖子呢?

另外,承认 BB5 是一个问题:

https://github.com/jquery/jquery -移动/问题/1245

You don't say which version of jqmobile you are using. Version 4.1 was released April 7. You have to keep in mind that jqmobile is not even at Beta release yet, and that BB OS 5 is listed as B-grade (http://jquerymobile.com/gbs/) so it is getting less attention.

You probably have other ajax in your app to deal with but login is so simple why not restructure as a simple form post?

Also, an admission that BB5 is a problem:

https://github.com/jquery/jquery-mobile/issues/1245

红衣飘飘貌似仙 2024-11-07 01:53:11

最后,经过一周的强烈挫败感,我得到了解决方案......

您需要在 BrowserField 中允许跨域访问,如下所示。

BrowserFieldConfig config=new BrowserFieldConfig();
config.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);
 config.setProperty( BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE );
 config.setProperty(BrowserFieldConfig.ALLOW_CS_XHR, Boolean.TRUE);//allow cross-domain
 browser=new BrowserField(config);

Finally after one week of intense frustration i got the solution....

You need to allow cross domain access in your BrowserField as follows.

BrowserFieldConfig config=new BrowserFieldConfig();
config.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);
 config.setProperty( BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE );
 config.setProperty(BrowserFieldConfig.ALLOW_CS_XHR, Boolean.TRUE);//allow cross-domain
 browser=new BrowserField(config);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文