TypeError:无法读取属性' id'未定义的

时间:2015-07-20 06:55:58

标签: javascript jquery html node.js

我试图从html文件发送数据到nodejs服务器下面发送的数据是我的html文件和nodejs文件我上面的类型错误请帮忙

在脚本代码中我将数据从html文件发送到nodejs服务器

Index.html文件

<html>
<head>
<title>contacts</title>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">  </script>


<script>
$(document).ready(function(){




     var id,name,phone,email;
   $("#save").click(function(){

       id=$("#id").val();
       name=$("#name").val();
       phone=$("#phone").val();
       email=$("#email").val();
        $.post("http://localhost:8083/save",{id:id,name:name,phone:phone,email:email},function(data){



        });
    });
});
</script>

</head>

<body>
  <div id="a">
  </div>
<form class="form-horizontal">
  <div class="form-group">
    <label for="exampleInputID" class="col-sm-2 control-label">ID</label>
    <input type="ID" class="form-control" id="id" placeholder="ID">
  </div>
  <div class="form-group">
    <label for="exampleInputName" class="col-sm-2 control-label">Name</label>
    <input type="Name" class="form-control" id="name" placeholder="Name">
  </div>
 <div class="form-group">
    <label for="exampleInputPhone" class="col-sm-2 control-label">Phone</label>
    <input type="Phone" class="form-control" id="phone" placeholder="Phone">
  </div>
  <div class="form-group">
    <label for="exampleInputEmail1" class="col-sm-2 control-label">Email address</label>
    <input type="email" class="form-control" id="email" placeholder="Email">
  </div>




  <input id='save' type="button" value="save"  >
</form>

 <input type="button" id='get' value="Get" style="float: right;">
 </div>
</body>

</html>

Nodejs File


var express = require('express');
var app = express();
var http = require("http");
var mysql =  require('mysql');
var connection =  mysql.createConnection({
    host : 'localhost',
 user : 'root',
 password:''
});

connection.connect();

connection.query('use mysql');
var strQuery = 'select * from Person';
 app.use(express.static(__dirname + '/../Desktop'));


 app.post('/save',function(req,res){
  //res.setEncoding('utf8');
  var user_id=JSON.stringify(req.body.id);
  var user_name=JSON.stringify(req.body.name);

// this is my post request above



  console.log("ID = "+user_id+", name is "+user_name+", phone no. is "+user_phone+", email is "+user_email);

});

 app.listen(8083);
console.log('server running\n');

1 个答案:

答案 0 :(得分:2)

这是你的问题:

 <input type="ID" class="form-control" id="id" placeholder="ID">

输入标记仍然没有此类type="ID"。对于有效类型的输入标记,您可以参考http://www.w3schools.com/html/html_form_input_types.asp

有一些HTML5添加了几种新的输入类型:

color
date
datetime
datetime-local
email
month
number
range
search
tel
time
url
week