2015-01-06 8 views
0

게시물을 게시 할 때 문제가 있습니다. 내 app.get 완벽하게 작동하지만 게시물에 대해 동일한 말할 수 없습니다. 이것은 내 server.js노드 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!"); 
     } 
     }); 
} 

입니다. 나는 응답을 얻지 못한다. 단지 아무 일도 일어나지 않는다. 얻을 수있는 작품이 정말 훌륭하기 때문에 이상합니다. 감사!

업데이트 : 서버 측이 훌륭하게 작동한다는 것을 알고 있습니다. 문제는 appjs 코드에 있습니다. 아약스 호출을 건너 뜁니다.

+0

클라이언트 코드가 실행 되었습니까? JSON.stringify (newOrder)'데이터 변수를 문자열화할 필요가 없으므로. 어떻게 전달할 수 있을까요?'data : newOrder, ' – czerasz

+0

네, 실행됩니다. onclick = "addOrder()"가있는 버튼 일뿐입니다. 사실 저는 아약스 바로 전에 경고를받을 수 있지만 그게 전부입니다. 다른 건 없어. –

+0

요청이 서버에 도착합니까 (브라우저 네트워크 모니터에 체크인)? 그렇다면 서버가 디버그 메시지를 반환합니까? – czerasz

답변

0

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% 

다음 명령으로

// 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); 

:

나는 아래 예제를 테스트했다.

+0

나는 app.listen (8020)을 가지고있다. 아직 작동하지 않습니다. 당신의 코드를 시험해 보도록하겠습니다. –

+0

아직 작동하지 않습니다. 아약스는 전화를받지 않습니다. 이상하다. –

+0

자, 코드로 작업 할 수있게되었습니다. 경고에 문제가있었습니다. 이제 curl -v -XPOST 'localhost : 8020/server/orders'-d "name = 이름 값 및 성 = lastname 값 & 전화 = 전화 값 및 주소 = 주소 값 & queso = queso 값 및 vainilla = vainilla 값 및 날짜 = 날짜 값을 사용할 때 작동합니다. 아약스에서 전화 왔을 때. 그것은 여전히 ​​서버가 잘된다는 것을 의미하는 아약스를 호출하지 않고 문제는 addOrder 메소드에있다. –

0

jQuery의 HTML에 포함을 넣는 것을 잊어 버렸습니다. 이것이 바로 아약스 블록 코드가 건너 뛰었던 이유입니다.