简体   繁体   English

节点js表达3 app.post不起作用

[英]node js express 3 app.post not working

I am having problems when trying to do a post. 尝试发布帖子时遇到问题。 my app.get works perfectly, but I cannot say the same for the post. 我的app.get可以完美运行,但是我不能在帖子中说同样的话。 This is my server.js 这是我的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 这是我的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. 没有调用ajax。 I do not get a response, just nothing happens. 我没有得到回应,什么都没有发生。 It's strange because the get works really great. 这很奇怪,因为get真的很棒。 Thanks! 谢谢!

UPDATE: I do know that the server side works great. 更新:我确实知道服务器端工作得很好。 The problem is in the appjs code. 问题出在appjs代码中。 It is skipping the ajax call. 它正在跳过ajax调用。

I think you forgot the app.listen(8020); 我认为您忘记了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代码块的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM