weixin_33701617 2017-06-12 05:15 采纳率: 0%
浏览 34

AJAX-覆盖提交功能

I am trying to create login page but I stuck on sending data to Cherrypy as a JSON. Trying to override submit button because AJAX does not send any data when I am pressing the button.

Old Code Deleted


@EDIT I have changed the code and now I am not sending any data to server (I assume that cuz there is nothing in console after pressing the button)

My .js

$("#myForm").click(function(){
    var dataString = {};
    dataString.login = $("#login").val();
    dataString.password = $("#password").val();
    $.ajax({
    type: 'POST',
    url: 'http://localhost:8080/login',
    data: JSON.stringify(dataString),
    contentType: 'application/json',
    dataType: 'json',
    success: function(response) {
        console.log(response);
    },
    error: function(response) {
            console.log(response);
    }
  });
});

My .html

<!DOCTYPE html>
<html>
  <head>
    <link href="style.css" rel="stylesheet">
    <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script src="example.js"></script>
  </head>
<body>
<div class="login-page">
  <div class="form">
      <form id="myForm" class="login-form">
        <input type="text" name="login" id="login" placeholder="login"/>
        <input type="password" name="password" id="password" placeholder="password"/>
        <button form="myForm" type="submit">Login</button>
        <p class="message">Not registered? <a href="#">Create an account</a></p>
      </form>
  </div>
</div>
</body>
</html>

Login Page (data not encrypted?)

PIC

  • Password in the link?

My Console (empty?)

PIC

  • No Login call

What do I do wrong here?

  • 写回答

4条回答 默认 最新

  • weixin_33704591 2017-06-12 05:17
    关注

    You want to do a post request (you have to handle post on server side as well):

    type: 'POST',
    

    Also put this inside your submit function:

    var dataString = {};
    dataString.login = $("#login").val();
    dataString.password = $("#password").val();
    

    So the code becomes :

    <form id="myForm" class="login-form">
      <input type="text" id="login" placeholder="login"/>
      <input type="password" id="password" placeholder="password"/>
      <button class="button_submit">login</button>
      <p class="message">Not registered? <a href="#">Create an account</a></p>
    </form>
    
    $("#myForm").submit(function(){
        var dataString = {};
        dataString.login = $("#login").val();
        dataString.password = $("#password").val();
        $.ajax({
        type: 'POST',
        url: 'http://localhost:8080/login',
        data: JSON.stringify(dataString),
          contentType: 'application/json',
        dataType: 'json',
        success: function(response) {
            console.log(response);
        },
        error: function(response) {
                console.log(response);
        }
      });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真
  • ¥15 关于#c语言#的问题,请各位专家解答!