mhtt
V2EX  ›  问与答

如何才能优雅的使用 jquery?

  •  
  •   mhtt · Apr 28, 2016 · 1992 views
    This topic created in 3752 days ago, the information mentioned may be changed or developed.

    比如一个登录页面,从源码可以看出来,放入了很多 js 代码

    <script type="text/javascript" src="js/jquery-1.12.0.min.js" charset="utf-8"></script>
    <script type="text/javascript">  
        $(document).ready(function(){  
        $('#submit').bind('click', logininto);
        });
        function logininto(){
            var username = $('#username').val();
            var password = $('#password').val();
            var data = '{"username": "'+ username +'","password": "'+ password +'"}';
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "login.php",
                dataType: "json",
                anysc: false,
                data: data,
                success: function(data){
                    //更多代码
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    //更多代码
                }
            }); 
        }  
    </script>
    

    这样感觉大量请求的代码等会被看到,太不优雅了,有什么其他优雅的方式吗?

    6 replies    2016-04-28 23:23:05 +08:00
    holyghost
        1
    holyghost  
       Apr 28, 2016
    去知乎问好吗
    Light3
        2
    Light3  
       Apr 28, 2016
    去知乎问好吗 你发的帖子对我们好没有帮助阿。。
    virusdefender
        3
    virusdefender  
       Apr 28, 2016
    你需要的是 mvvm 框架, vue 或者 angularjs 都行
    airyland
        4
    airyland  
       Apr 28, 2016
    无论你用何种形式,都会被看到请求。
    wdhwg001
        5
    wdhwg001  
       Apr 28, 2016 via iPhone
    就代码来说,除了拼接 json 可能会有天大的问题以外都是小事情,想再优雅也无非是改改命名,改改缩进,用 post()而不是 ajax()…

    …然后这坨东西就全变了( x
    ayiis
        6
    ayiis  
       Apr 28, 2016
    封装$.ajax 这类常用的方法
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3123 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 97ms · UTC 00:28 · PVG 08:28 · LAX 17:28 · JFK 20:28
    ♥ Do have faith in what you're doing.