如何在身体负荷上调用功能

时间:2018-01-08 06:35:35

标签: javascript jquery json ajax

这里我试图在身体负载上调用oauth2_login()函数,它应该登录用户。截至目前,我正在将数据库中存在的值硬编码到用户名和密码字段中。

<!DOCTYPE html>
<html>
<!--
* Please see the included README.md file for license terms and conditions.
-->
<head>
    <title>Blank Cordova Mobile App Template</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <!-- see http://webdesign.tutsplus.com/tutorials/htmlcss-tutorials/quick-tip-dont-forget-the-viewport-meta-tag -->
    <!-- <meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1"> -->
    <meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=no">
    <!-- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes, minimum-scale=1, maximum-scale=2"> -->
    <!-- <link rel="stylesheet" href="css/app.css">-->
    <style>
        /* following two viewport lines are equivalent to meta viewport statement above, and is needed for Windows */
        /* see http://www.quirksmode.org/blog/archives/2014/05/html5_dev_conf.html and http://dev.w3.org/csswg/css-device-adapt/ */
        @-ms-viewport {
            width: 100vw;
            min-zoom: 100%;
            zoom: 100%;
        }

        @viewport {
            width: 100vw;
            min-zoom: 100% zoom: 100%;
        }

        @-ms-viewport {
            user-zoom: fixed;
            min-zoom: 100%;
        }

        @viewport {
            user-zoom: fixed;
            min-zoom: 100%;
        }
        /*@-ms-viewport { user-zoom: zoom ; min-zoom: 100% ; max-zoom: 200% ; }   @viewport { user-zoom: zoom ; min-zoom: 100% ; max-zoom: 200% ; }*/

        .dispInline,
        .dispInlineLabel {
            display: inline-grid;
            border-bottom-width: 0;
        }

        .dispInlineLabel {
            min-width: auto;
        }

        .dispInline {
            min-width: auto;
        }

        .clearFloats {
            clear: both;
        }

        #tck_id {
            padding-bottom: 10px;
        }
    </style>
    <!-- See explanation at the bottom of this file for info regarding placement of JS libraries. -->
    <!-- These library references (below) are just examples to give you the general idea... -->
    <!-- <script src="lib/mc/hammer.js"></script> -->
    <!-- <script src="lib/ft/fastclick.js"></script> -->
    <!-- <script src="lib/jq/jquery.js"></script> -->
    <!--    <link rel="stylesheet" href="css/New.css" type="text/css">-->
    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body onload="oauth2_login()">
<div data-role="page" id="paper">
    <div data-role="header" data-theme="b" data-position="fixed">
        <a href="#home" data-icon="arrow-l" data-iconpos="notext" data-direction="reverse" onclick="location.href='home.html'">Back</a>
        <h1>Hello</h1>
    </div>
    <div data-role="content">
        <!--       <div class="clearFloats"></div>-->
        <!--  <button onclick="getLocation()">Try It</button>-->
        <p id="demo"></p>
        <div data-role="footer" data-position="fixed" data-theme="b">
            <!-- fixed footer -->
            <div data-role="navbar">
            </div>
        </div>
    </div>
</div>

<script>
    function oauth2_login() {
        var email = "abhigyan.gaurav@99rstreet.com";
        var password = "abhigyan@123";
        var dataString = "User=" + email + "&Pass=" + password + "&login=";
        if ($.trim(email).length > 0 & $.trim(password).length > 0) {
            $.ajax({
                type: "POST",
                url: "http://35.201.138.23:8080/APIPRODUCTION/OperationJSP/operations.jsp",
                data: dataString,
                crossDomain: true,
                cache: false,
                beforeSend: function() {
                    $("#callAjax").html('Connecting…');
                },
                success: function(data) {
                    var Data = $.parseJSON(data);
                    if (Data.success == '1') {
                        localStorage.login = "true";
                        //localStorage.email = email;
                        var theName = $.trim($("#theName").val());
                        var thePassword = $.trim($("#thePassword").val());
                        localStorage.setItem("PMUsername", theName);
                        localStorage.setItem("PMPassword", thePassword);
                        localStorage.setItem("PMResponse", Data.success);
                        alert("Login Successful");
                        window.location.href = "home.html";
                    } else if (Data.success == '0') {
                        // document.getElementById("waiting").innerHTML = " ";
                        alert("Wrong User");
                        $("#callAjax").html('Login');
                    }
                }
            });
        }
        return false;
    });

    function Loading() {
        document.getElementById("waiting").innerHTML = "Loading...";
    }
</script>
<script src="cordova.js"></script>
<!-- for your event code, see README and file comments for details -->
<script src="js/paper.js"></script>
<!-- for your init code, see README and file comments for details -->
<script src="js/init-app.js"></script>
<!-- normalizes device and document ready events, see file for details -->
<script src="xdk/init-dev.js"></script>
<!-- IMPORTANT: Do not include a weinre script tag as part of your release builds! -->
<!-- Place your remote debugging weinre script URL here, if it does not work above. -->
</body>
</html>

我需要一些帮助和建议。这有什么问题?以上是我正在使用的所有代码。 感谢

4 个答案:

答案 0 :(得分:2)

方法1: 使用纯javascript(dom事件)

<body onload="oauth2_login()">

方法2: 或者使用jQuery

$(document).ready(function() {
  oauth2_login()
});

答案 1 :(得分:0)

在jquery的文档就绪函数中调用函数:

$(document).ready(function() {
  oauth2_login();
});

答案 2 :(得分:0)

实际上你得到2个控制台错误。 (当我在我的机器上执行相同的代码时。)

在方法);之前您还有一个Loading()。删除它,它会工作。 只是注释了方法oauth2_login()中的所有行,并安排了一条消息来测试及其工作。

注意:您的格式错误对开发人员不利。请善意改变它。

:)快乐编码:)

<!DOCTYPE html>
<html>
<!--
  * Please see the included README.md file for license terms and conditions.
  -->
<head>
    <title>Blank Cordova Mobile App Template</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">

    <!-- see http://webdesign.tutsplus.com/tutorials/htmlcss-tutorials/quick-tip-dont-forget-the-viewport-meta-tag -->
    <!-- <meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1"> -->
    <meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=no">
    <!-- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes, minimum-scale=1, maximum-scale=2"> -->
   <!-- <link rel="stylesheet" href="css/app.css">-->
    <style>
        /* following two viewport lines are equivalent to meta viewport statement above, and is needed for Windows */
        /* see http://www.quirksmode.org/blog/archives/2014/05/html5_dev_conf.html and http://dev.w3.org/csswg/css-device-adapt/ */
        @-ms-viewport { width: 100vw ; min-zoom: 100% ; zoom: 100% ; }          @viewport { width: 100vw ; min-zoom: 100% zoom: 100% ; }
        @-ms-viewport { user-zoom: fixed ; min-zoom: 100% ; }                   @viewport { user-zoom: fixed ; min-zoom: 100% ; }
        /*@-ms-viewport { user-zoom: zoom ; min-zoom: 100% ; max-zoom: 200% ; }   @viewport { user-zoom: zoom ; min-zoom: 100% ; max-zoom: 200% ; }*/
        .dispInline, .dispInlineLabel{
    display: inline-grid;
    border-bottom-width:0;

}
.dispInlineLabel{
    min-width: auto;


}
.dispInline{
     min-width: auto;

}
.clearFloats{
    clear:both;
}

        #tck_id
        {
            padding-bottom: 10px;
        }


    </style>

    <!-- See explanation at the bottom of this file for info regarding placement of JS libraries. -->
    <!-- These library references (below) are just examples to give you the general idea... -->
    <!-- <script src="lib/mc/hammer.js"></script> -->
    <!-- <script src="lib/ft/fastclick.js"></script> -->
    <!-- <script src="lib/jq/jquery.js"></script> -->

<!--    <link rel="stylesheet" href="css/New.css" type="text/css">-->
     <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>


<body onload="oauth2_login()">
  <div data-role="page" id="paper">
      <div data-role="header" data-theme="b" data-position="fixed">
           <a href="#home" data-icon="arrow-l" data-iconpos="notext" data-direction="reverse" onclick="location.href='home.html'">Back</a>
         <h1>Hello</h1>
      </div>
      <div data-role="content">
          <p id="demo"></p>
          <div data-role="footer" data-position="fixed" data-theme="b"> <!-- fixed footer -->
              <div data-role="navbar"></div>
            </div>
        </div>
    </div>
    <script>

    function oauth2_login()
    {
        console.log("Here");
//      var email = "abhigyan.gaurav@99rstreet.com";
//      var password = "abhigyan@123";
//        var dataString="User="+email+"&Pass="+password+"&login=";
//        if($.trim(userName).length>0 & $.trim(password).length>0)
//        {
//            $.ajax({
//            type: "POST",
//
//            url: "http://35.201.138.23:8080/APIPRODUCTION/OperationJSP/operations.jsp",
//            data: dataString,
//            crossDomain: true,
//            cache: false,
//            beforeSend: function(){ $("#callAjax").html('Connecting…');},
//            success: function(data){
//                    var Data = $.parseJSON(data);
//                    if(Data.success=='1')
//                    {
//                        localStorage.login="true";
//                        //localStorage.email=email;
//                        var theName = $.trim($("#theName").val());
//                        var thePassword = $.trim($("#thePassword").val());
//                        localStorage.setItem("PMUsername",theName);
//                        localStorage.setItem("PMPassword",thePassword);
//                        localStorage.setItem("PMResponse",Data.success);
//                        alert("Login Successful");
//                        window.location.href = "home.html";
//                    }
//                    else if(Data.success=='0')
//                    {
//
//                        // document.getElementById("waiting").innerHTML = " ";
//                        alert("Wrong User");
//                        $("#callAjax").html('Login');
//                    }
//                }
//            });
//        }
        return false;
    }

    function Loading()
    {
        document.getElementById("waiting").innerHTML = "Loading...";
    }
</script>




    <script src="cordova.js"></script>

    <!-- for your event code, see README and file comments for details -->
    <script src="js/paper.js"></script>
    <!-- for your init code, see README and file comments for details -->
    <script src="js/init-app.js"></script>
    <!-- normalizes device and document ready events, see file for details -->
    <script src="xdk/init-dev.js"></script>

    <!-- IMPORTANT: Do not include a weinre script tag as part of your release builds! -->
    <!-- Place your remote debugging weinre script URL here, if it does not work above. -->
</body>

答案 3 :(得分:0)

您可以通过多种方式完成此操作:

  1. 如果使用jQuery,请在DOM准备好后调用该方法:

    $(document).ready(function(){
        oauth2_login();
    })
    
  2. 如果使用javascript,请在​​window.onLoad:

    上调用该方法
    window.onload = function(){
         oauth2_login();
    }
    
  3. 您可以将整个oauth2_login()作为自调用函数。我使用jQuery,你可以在DOM准备好后定义它。

    (function(){
         // code of oauth2_login goes here...
    })();
    
  4. 其他

    <body onload="oauth2_login()"></body>