2017-09-13 50 views
-3

這是我在form.html形式發送數據的,到服務器的NodeJS,我要做一個Ajax調用類型後我如何從HTML表單使用JavaScript只

<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> 
+0

你爲發送Ajax請求是什麼? – bhansa

+0

看到這個例子:https://gist.github.com/diorahman/1520485 – bhansa

+0

我只能用javascript來做 –

回答

0

我解決它以這種方式

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)); 
         } 
        }); 

       }); 
      }); 

相關問題