javascript - 如何在 body 负荷时调用函数

标签 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>


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

最佳答案

实际上,您会收到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>

关于javascript - 如何在 body 负荷时调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48145348/

相关文章:

javascript - 如何获取 dom 创建的单选按钮的实际标签值/文本?

java - 在 Java 中使用 JSON 进行 http POST 后的奇怪响应

javascript - 使用php将ajax发送的数据写入带有数组的json文件

javascript - 在javascript中将字符串转换为数组 "datastatusMonthly[0]"

javascript - 尝试更改 Textarea 的值(使用 Skulpt)

jQuery - 从属性获取文本

javascript - 取消 ng-change 事件

javascript - 如何通过可点击的列在同一页面中搜索值

javascript - 循环引用阻止 jsonwebtoken.sign JSON.stringify 的原因

java - 为什么我的 JSON 响应前面带有 {} &&