2015-01-06 163 views
0

我在嘗試發佈帖子時遇到問題。我的app.get可以很好地工作,但我不能說這個帖子是一樣的。這是我的server.jsnode js express 3 app.post not working

// Express is the web framework 
var express = require('express'); 
var app = express(); 
app.use(express.bodyParser()); 
app.configure(function() { 
    app.use(allowCrossDomain); 
}); 

app.post('/server/orders',express.bodyParser(), function(req, res) { 
    alert("Estamoooo"); 
    console.log("POST Order"); 
    var client = mysql.createConnection({ 
    host  : 'localhost', 
    database : "DB", 
    user  : 'root', 
    password : 'XXXX' 
    }); 
    client.connect(); 


    var name = req.body.name; 
    var lastname = req.body.lastname; 
    var telephone = req.body.telephone; 
    var address = req.body.address; 
    var queso = req.body.queso; 
    var vainilla = req.body.vainilla; 
    var date = req.body.date; 


    var query = client.query("insert into orden(name,lastname,telephone,address,queso,vainilla,date) values('" 
    + name + "', '" + lastname +"','"+ telephone +"' , "+ address + ", '" + queso + "', "+ vainilla +", '" + date 
    +"');"); 
    query.on("end", function (result) { 
     client.end(); 
     res.json(true); 
    }); 

}); 

,這是我appjs.js

function addOrder(){ 
    var newOrder = {}; 
    newOrder.name = document.getElementById("nombre").value; 
    newOrder.lastname = document.getElementById("apellido").value; 
    newOrder.telephone = document.getElementById("telefono").value; 
    newOrder.address = document.getElementById("direccion").value; 
    newOrder.queso = document.getElementById("cantidadQueso").value; 
    newOrder.vainilla = document.getElementById("cantidadVainilla").value; 
    newOrder.date = document.getElementById("fecha").value; 

    var newOrderJSON = JSON.stringify(newOrder); 
    alert("New Order: " + newOrderJSON); 
    $.ajax({ 
     url : "http://192.168.0.1:8020/server/orders", 
     method: 'post', 
     data : newOrderJSON, 
     contentType: "application/json", 
     dataType:"json", 
     success : function(data, textStatus, jqXHR){ 
      alert("Data Added!!!"); 
     }, 
     error: function(data, textStatus, jqXHR){ 
      alert("Data could not be added!"); 
     } 
     }); 
} 

阿賈克斯不會被調用。我沒有得到迴應,只是沒有任何反應。這很奇怪,因爲get的作品真的很棒。謝謝!

更新:我知道服務器端工作得很好。問題出在appjs代碼中。它正在跳過ajax呼叫。

+0

您的客戶代碼是否被執行?順便說一句,你不需要串數據變量:'JSON.stringify(newOrder)'。只是通過它是如何的:'數據:newOrder,' – czerasz

+0

是的它被執行。只是一個按鈕,它有一個onclick =「addOrder()」實際上我可以在ajax之前得到警報,但那就是它。沒有其他的。 –

+0

請求是否到達服務器(檢入您的瀏覽器網絡監視器)?如果是,服務器是否會返回任何調試消息? – czerasz

回答

0

我想你忘了app.listen(8020);的一部分。

我測試下面的例子:

// Express is the web framework 
var express = require('express'); 
var app = express(); 
app.use(express.bodyParser()); 

app.post('/server/orders',express.bodyParser(), function(req, res) { 
    console.log("POST Order"); 

    var name = req.body.name; 
    var lastname = req.body.lastname; 
    var telephone = req.body.telephone; 
    var address = req.body.address; 
    var queso = req.body.queso; 
    var vainilla = req.body.vainilla; 
    var date = req.body.date; 

    console.log(name, lastname, telephone, address, queso, vainilla, date); 

    res.send('works'); 
}); 

app.listen(8020); 

用下面的命令:

curl -v -XPOST 'localhost:8020/server/orders' -d "name=name value&lastname=lastname value&telephone=telephone value&address=address value&queso=queso value&vainilla=vainilla value&date=date value" 
* Hostname was NOT found in DNS cache 
* Trying 127.0.0.1... 
* Connected to localhost (127.0.0.1) port 8020 (#0) 
> POST /server/orders HTTP/1.1 
> User-Agent: curl/7.35.0 
> Host: localhost:8020 
> Accept: */* 
> Content-Length: 145 
> Content-Type: application/x-www-form-urlencoded 
> 
* upload completely sent off: 145 out of 145 bytes 
< HTTP/1.1 200 OK 
< X-Powered-By: Express 
< Content-Type: text/html; charset=utf-8 
< Content-Length: 5 
< Date: Tue, 06 Jan 2015 16:42:35 GMT 
< Connection: keep-alive 
< 
* Connection #0 to host localhost left intact 
works% 

正如你可以看到它的工作原理。

+0

我有app.listen(8020); 仍然沒有工作。讓我試試你的代碼。 –

+0

仍然沒有工作。 ajax沒有被調用。這很奇怪 –

+0

好吧,讓它與您的代碼一起工作。我遇到了警報問題。當我使用curl -v -XPOST'localhost:8020/server/orders'-d「name = name value&lastname = lastname value&telephone = telephone value&address = address value&queso = queso value&vainilla = vainilla value&date = date value」時,當從ajax調用時。它仍然沒有調用ajax,這意味着服務器沒問題,問題在於addOrder方法。 –

0

我忘了把包括在我的html jquery,這就是爲什麼ajax代碼塊被跳過。