Javascript没有正确调用API

时间:2018-02-08 17:32:49

标签: javascript html

我正在尝试让我的网站调用我的登录API,我已经从一个单独的应用程序和Postman测试,它运行正常。但是,当我通过我的网站运行它时,它不会使用html输入项中的实际值调用API。

以下是我的属性HTML:

  <div class="container">
    <label for="uname"><b>Username</b></label>
    <input id= "username" type="text" placeholder="Enter Username" name="uname" required>

    <label for="psw"><b>Password</b></label>
    <input id= "password" type="password" placeholder="Enter Password" name="psw" required>

    <button id="loginButton" type="button" class=""">login</button>
    <label>
      <input type="checkbox" checked="checked" name="remember"> Remember me
    </label>
  </div>

以下是我的网站API调用的代码:

<script type="text/javascript">
          document.getElementById("loginButton").onclick = function () {
            var xhttp = new XMLHttpRequest();

           console.log("login button clicked");
           var usr = document.getElementById("username").value;
           var psw = document.getElementById("password").value;

           console.log(usr);
           console.log(psw);

           xhttp.open("GET", "http://serverAddress/checkHash/"+usr+"/"+psw+"/", true);
           xhttp.setRequestHeader("Content-type", "application/json");
           xhttp.send();
           var response = (xhttp.responseText);
           console.log("user logged in");
           console.log("the response is:" + response);

           //var value = (usr.concat(psw));
           //console.log('concat value of both usr and psw is:');
           //console.log(value);
           if(response != "no") {
              //this means the credentials are right
              localStorage.setItem("session", usr);
              location.href = "userSearch.php";
           } else {
              window.alert("Incorrect credentials");
           }
          };
      </script> 

以下是我的服务器代码:

app.post('/createPhysician/', function(req, res) {

    console.log("below is the req body for createPhysician");
    console.log(req.body);

    var createPromise = interact.createPhysician(
        req.body.firstName,
        req.body.lastName,
        req.body.yearNum,
        req.body.position,
        req.body.isAttending,
        req.body.highRiskTrained);

    createPromise.then(function(createResponse) {
        res.json("successful"); // returns the physicianID for the createUsers
    }).catch(function(err) {
        console.log(err);
        console.log(req.body);
        res.json("Terrible job you botched it");
    }); 
});

下面是我的交互式sql文件:

   createPhysician: function(
        firstName,
        lastName,
        yearNum,
        position,
        isAttending,
        highRiskTrained)  {
        var qry = "insert into Physician (firstName, lastName, yearNum, position, isAttending, highRiskTrained) values ('"+firstName+"', '"+lastName+"', "+yearNum+", '"+position+"', "+isAttending+", "+highRiskTrained+");";
        console.log("below is query ran in DBINteract");
        console.log(qry);
        return runQuery(qry);
    }

我得到的错误如下:

below is the username given to server.js
[object HTMLInputElement]
below is the value of pass from app
[object HTMLInputElement]
below is the value from server side
TypeError: Cannot read property 'password' of undefined

0 个答案:

没有答案