I am having problems when trying to do a post. my app.get works perfectly, but I cannot say the same for the post. This is my 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);
});
});
and this is my 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!");
}
});
}
The ajax is not being called. I do not get a response, just nothing happens. It's strange because the get works really great. Thanks!
UPDATE: I do know that the server side works great. The problem is in the appjs code. It is skipping the ajax call.
I think you forgot the app.listen(8020);
part.
I tested the example below:
// 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);
With the following command:
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%
As you can see it works.
我忘了把include放在我的jquery的html上,这就是为什么跳过了ajax代码块的原因。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.