Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
POST请求后,don';t进入成功回调(javascript)_Javascript_Ajax_Post_Request_Response - Fatal编程技术网

POST请求后,don';t进入成功回调(javascript)

POST请求后,don';t进入成功回调(javascript),javascript,ajax,post,request,response,Javascript,Ajax,Post,Request,Response,我的问题是请求post请求的页面不见了,然后只显示来自服务器的JSON数据字符串。请让我知道如何在不重定向(?)页面的情况下获取响应数据 res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({message : "success"})); 客户发布请求 <!DOCTYPE html> <html > <head> <meta chars

我的问题是请求post请求的页面不见了,然后只显示来自服务器的JSON数据字符串。请让我知道如何在不重定向(?)页面的情况下获取响应数据

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

<!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>
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({message : "success"}));
结果

{“message”:“success”}

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

res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({message : "success"}));
 <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>

$(文档).ready(函数(){
$(“#提交”)。单击(功能(e){
e、 预防默认值();
变量形式_数据={
用户id:$(“#用户id”).val(),
user_pw:$(“#user_pw”).val(),
这是阿贾克斯:1
};
$.ajax({
类型:“POST”,
url:“/”,
数据:JSON.stringify(form_data),
contentType:“应用程序/json”,
数据类型:“json”,
成功:功能(响应){
console.log(响应);//->它不工作。
},
错误:函数(响应){
console.log('fail');
}
});
});
});

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

res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({message : "success"}));
 <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>

$(文档).ready(函数(){
$(“#提交”)。单击(功能(e){
e、 预防默认值();
变量形式_数据={
用户id:$(“#用户id”).val(),
user_pw:$(“#user_pw”).val(),
这是阿贾克斯:1
};
$.ajax({
类型:“POST”,
url:“/”,
数据:JSON.stringify(form_data),
contentType:“应用程序/json”,
数据类型:“json”,
成功:功能(响应){
console.log(响应);//->它不工作。
},
错误:函数(响应){
console.log('fail');
}
});
});
});

如果希望使用Ajax请求处理帖子,则不应使用HTML发布

res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({message : "success"}));
按如下方式更改您的HTML:

res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({message : "success"}));
<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方法,并删除按钮字段上的“提交”类型。
我添加了type=“button”,但如果css处理呈现,您也可以使用type=“text”。

如果您希望使用Ajax请求处理帖子,则不应使用HTML帖子

res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({message : "success"}));
按如下方式更改您的HTML:

res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({message : "success"}));
<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方法,并删除按钮字段上的“提交”类型。
我添加了type=“button”,但如果您的css处理渲染,您也可以使用type=“text”。

您能把html放在问题中吗?我认为您在提交表单时遇到了与所使用的html元素的行为相关的问题?您能把html放在问题中吗?我认为您在提交表单时遇到了与所使用的html元素行为相关的问题?谢谢您的帮助,正如您所说,我添加了preventDefault()函数。但它仍然不起作用。您是否有任何错误或页面仍在重定向?没有,只是没有任何更改。@JincheolKwon您是否已将
e
参数添加到参数列表中?谢谢您的帮助,正如您所说,我添加了preventDefault()函数。但它仍然不起作用。您是否有任何错误或页面仍在重定向?没有,只是没有更改。@JincheolKwon您是否已将
e
参数添加到参数列表中?
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({message : "success"}));