【问题标题】:How I can send data from html Form, to a nodejs Server using Javascript only如何仅使用 Javascript 将数据从 html 表单发送到 nodejs 服务器
【发布时间】:2018-02-22 01:57:36
【问题描述】:

这是我在form.html中的表格,我要做一个ajax调用类型的帖子

<div class= "container " >

    <div class="form-group ">
      <label for="id">Id</label>
      <input type="text" class="form-control" id="id" >
    </div>
    <div class="form-group">
      <label for="name">Nome</label>
      <input type="text" class="form-control" id="name">
    </div>
    <div class="form-group">
      <label for="pwd">Prezzo</label>
      <input type="text" class="form-control" id="prezzo">
    </div>
    <input type="submit" value="invia dati" class="btn btn-primary" id="button" />
  </div>

【问题讨论】:

标签: javascript node.js forms server client


【解决方案1】:

我是这样解决的

var $id = $('#id');
var $name = $('#name');
var $prezzo = $('#prezzo');


$(function(){
               $('#button').click(function(e){
                   e.preventDefault();
                   console.log('select_link clicked');

                                   var object = {
                       id:$id.val(),
                       name:$name.val(),
                       prezzo: $prezzo.val()
                     };


         $.ajax({
           type: 'POST',
           data: JSON.stringify(object),
           contentType: 'application/json',
           url: 'http://localhost:3000/router/newProducts',
           success: function(data) {
                   console.log('success');
                   console.log(JSON.stringify(object));
                       }
                   });

               });
           });

【讨论】:

    猜你喜欢
    • 2017-12-15
    • 2019-06-27
    • 2016-03-18
    • 2019-09-01
    • 2011-07-28
    • 1970-01-01
    • 2019-12-05
    • 2017-03-29
    • 1970-01-01
    相关资源
    最近更新 更多