POST请求后,不要成功回拨(javascript)

时间:2017-02-03 08:32:27

标签: javascript ajax post request response

我的问题是请求发布请求的页面消失了,然后只显示一个来自服务器的JSON数据字符串。请告诉我如何在不重定向(?)页面的情况下获取响应数据。

客户端POST请求

<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>Login Form</title>

  <script
    src="https://code.jquery.com/jquery-3.1.1.min.js"
    integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
    crossorigin="anonymous"></script>
  <link rel="stylesheet" type="text/css" href="../../semantic/dist/semantic.min.css">
  <link rel="stylesheet" type="text/css" href="../../semantic/dist/login.css">
  <script
    src="https://code.jquery.com/jquery-3.1.1.min.js"
    integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
    crossorigin="anonymous"></script>
  <script src="semantic/dist/semantic.min.js"></script>


  <script type="text/javascript">
      $(document).ready(function() {
        $("#submit").click(function() {
            var form_data = {
                user_id: $("#user_id").val(),
                user_pw: $("#user_pw").val(),
                is_ajax: 1
            };
            $.ajax({
                type: "POST",
                url: '/',
                data: JSON.stringify(form_data),
                contentType: "application/json",
                dataType: "json",
                success: function(response) {
                    console.log('test');
                },
                error : function(response){
                    console.log('test');
                }
            });
        });
      });
      </script>
</head>

<body>
  <div class="ui one column center aligned grid">
<div class="column six wide form-holder">
  <h2 class="center aligned header form-head">Sign in</h2>
  <div class="ui form">
  <form class="form-signin" method="post">
    <div class="field">
      <input type="text" name = "user_id" placeholder="Email">
    </div>
    <div class="field">
      <input type="password" name = "user_pw" placeholder="password">
    </div>
    <div class="field">
      <input type="submit" value="sign in" class="ui button large fluid green">
    </div>
    <div class="field">
      <CENTER><h5><a href = "/register">Register</a></h5></CENTER>
    </div>
    </form>
  </div>
</div>

服务器POST响应

res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({message : "success"}));

结果
{“message”:“成功”}

2 个答案:

答案 0 :(得分:2)

为了防止重定向,你应该使用preventDefault()函数。

 <script type="text/javascript">
      $(document).ready(function() {
        $("#submit").click(function(e) {
            e.preventDefault();
            var form_data = {
                user_id: $("#user_id").val(),
                user_pw: $("#user_pw").val(),
                is_ajax: 1
            };
            $.ajax({
                type: "POST",
                url: '/',
                data: JSON.stringify(form_data),
                contentType: "application/json",
                dataType: "json",
                success: function(response) {
                    console.log(response);   //->it is not working.
                },
                error : function(response){
                    console.log('fail');
                }
            });
        });
      });
      </script>

答案 1 :(得分:0)

如果要使用Ajax请求处理POST,则不应使用HTML发布。

按如下方式更改HTML:

<form class="form-signin">
    <div class="field">
      <input type="text" name = "user_id" placeholder="Email">
    </div>
    <div class="field">
      <input type="password" name = "user_pw" placeholder="password">
    </div>
    <div class="field">
      <input type="button" value="sign in" class="ui button large fluid green">
    </div>
    <div class="field">
      <CENTER><h5><a href = "/register">Register</a></h5></CENTER>
    </div>
</form>

因此,请删除表单标记上的POST方法,然后删除“提交”#39;在按钮字段上输入。 我添加了type =&#34; button&#34;但你也可以使用type =&#34; text&#34;如果你的css处理渲染。