簡體   English   中英

節點js表達3 app.post不起作用

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

嘗試發布帖子時遇到問題。 我的app.get可以完美運行,但是我不能在帖子中說同樣的話。 這是我的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代碼塊的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM