【问题标题】:POST request Not returning DATA to Express i.e. to serverPOST 请求不返回 DATA 到 Express 即服务器
【发布时间】:2019-10-04 08:37:26
【问题描述】:

我对 电子邮件和密码 有一个表单验证,如果匹配,它将替换为其他代码并在服务器控制台中显示 电子邮件和密码。程序流程是 onclick() 提交它去 ma​​in.js 并执行 newFunction() 它返回 POST 数据到服务器即 Server.js 并替换 html 内容。但在我的情况下,/Login url 已处理,但电子邮件和密码未进入我在 Server 中的 if 条件。 js*
if( emailid == 'abc@g.com' && password == '123' ) { console.log('Success'+emailid+password); }

我已经尝试了所有的 POST 请求处理程序,但都不起作用。

主页.html

     `<!--Login LogOut-->
    <div class="container" id ="Loginchange">
        <div class="row">
            <div class="col"  style="padding-center:20px; border-right: 3px solid #84b1aa;"><!--For middle line-->
                <!--Existing user-->
                <button class="btn btn-primary btn-lg btn-block active" type="button" data-toggle="collapse" data-target="#existingUser" aria-expanded="false" aria-controls="existingUser">
                    Existing User

                </button>
                <div class="collapse" id="existingUser">
                    <div class="card card-body"><!--Start of existing in button-->
                        <!--Alert with X-->
                        <div class="alert alert-warning alert-dismissible fade show" role="alert">
                            <strong>Hello Arkodian</strong> Please Log in.
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <!--Existing Form-->
                        <form>
                            <div class="form-group">
                                <label for="LoginEmail">Email address</label>
                                <input  type="email" class="form-control" id="LoginEmail" aria-describedby="emailHelp" placeholder="Enter email" data-toggle="tooltip" data-placement="top" title="Email Id" required>
                                <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
                            </div>
                            <div class="form-group">
                                <label for="LoginPassword">Password</label>
                                <input  type="password" class="form-control" id="LoginPassword" placeholder="Password" required>
                            </div>
                          <!--  <div class="form-group form-check">
                                <input type="checkbox" class="form-check-input" id="exampleCheck1">
                                <label class="form-check-label" for="exampleCheck1">Check me out</label>
                            </div> -->
                            <button onclick="newsFunction()" id="Login" class="btn btn-primary">Submit</button>
                        </form>
                    </div>
                </div>
            </div>`

main.js

  function newsFunction() {
     var Request = new XMLHttpRequest();
     Request.onreadystatechange = function(){
      if (Request.readyState == XMLHttpRequest.DONE){
        if (Request.status == 200){
             let  loginHtml = Request.responseText;
             let  ul = document.getElementById('Loginchange');
             ul.innerHTML =loginHtml ;
             console.log(loginHtml);
           }
       }
     };

     Request.open('POST', '/Login', false);
     Request.send({
        extid: document.getElementById('LoginEmail'),
        extpsd:document.getElementById('LoginPassword')
       });
    }

服务器.js

 var express = require('express');
 var bodyParser = require('body-parser');
 var app = express();
 app.use(bodyParser.urlencoded({extended :false}));
 app.use(bodyParser.json());

 app.post('/Login',function(req,res){
   var emailid = req.body.extid;
   var password = req.body.extpsd;
    if( emailid == 'abc@g.com' && password == '123' ){
        console.log('Success'+emailid+password);
      }
    else {
       console.log('user not found');
      }
    var loginHtml = `<p>Hello World</p>`
    res.status(200).send(loginHtml);
 });

【问题讨论】:

    标签: node.js forms express post xmlhttprequest


    【解决方案1】:

    尝试像这样对数据对象进行字符串化并添加标题"Content-Type": "application/json

    function newsFunction() {
         var Request = new XMLHttpRequest();
         Request.onreadystatechange = function(){
          if (Request.readyState == XMLHttpRequest.DONE){
            if (Request.status == 200){
                 let  loginHtml = Request.responseText;
                 let  ul = document.getElementById('Loginchange');
                 ul.innerHTML =loginHtml ;
                 console.log(loginHtml);
               }
           }
         };
    
         Request.open('POST', '/Login', false);
         Request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
         Request.send(JSON.stringify({
            extid: document.getElementById('LoginEmail').value,
            extpsd:document.getElementById('LoginPassword').value;
           }));
        }
    

    【讨论】:

    • @siddharthreddy 你在 server.js 上得到的 console.log(req.body.extid, req.body.extpsd);` 的值是什么
    • 来自 html 的电子邮件 id 和密码并输入 main.js 以响应发送到 server.js 他们去 /登录,但它没有进入if语句,不知道为什么我认为因为这段代码没有将数据从这里发送到server.js Request.send(JSON.stringify({ extid: document.getElementById('LoginEmail'), extpsd:document.getElementById('LoginPassword') }));
    • @siddharthreddy 在 server.js 文件中 if 条件之前尝试记录 req.body.extid、req.body.extpsd 的值。并检查它们是否未定义或为空?
    • @siddharthreddy 我也更新了答案试试
    • 你说更正它返回 null {},{} 但尝试了你的代码它没有进入 if 语句。
    【解决方案2】:

    It got worked when I added .value to the JSON object.

    【讨论】:

      猜你喜欢
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 2021-01-05
      相关资源
      最近更新 更多