节点 js express 3 app.post 不工作
node js express 3 app.post not working
我在尝试执行 post 时遇到问题。我的 app.get 工作完美,但我不能对 post 说同样的话。这是我的 server.js
// 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!");
}
});
}
未调用 ajax。我没有得到回应,只是什么也没发生。这很奇怪,因为 get 真的很棒。谢谢!
更新:我知道服务器端运行良好。问题出在 appjs 代码中。它正在跳过 ajax 调用。
我想你忘记了 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%
如您所见,它有效。
我忘记将 include 放在 jquery 的 html 上,这就是 ajax 代码块被跳过的原因。
我在尝试执行 post 时遇到问题。我的 app.get 工作完美,但我不能对 post 说同样的话。这是我的 server.js
// 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!");
}
});
}
未调用 ajax。我没有得到回应,只是什么也没发生。这很奇怪,因为 get 真的很棒。谢谢!
更新:我知道服务器端运行良好。问题出在 appjs 代码中。它正在跳过 ajax 调用。
我想你忘记了 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%
如您所见,它有效。
我忘记将 include 放在 jquery 的 html 上,这就是 ajax 代码块被跳过的原因。