2012-12-27 21 views
6

मैं JQuery के साथ AJAX पोस्ट अनुरोध को मेरे नोड.जेएस सर्वर में नोड.जेएस और एक्सप्रेस में बनाने का प्रयास कर रहा था, लेकिन यह काम नहीं करता है!नोड.जेएस के साथ JQuery अजाक्स पोस्ट अनुरोध और

var express = require('express') 
var app = express(); 
app.listen(8888); 

app.configure(function(){ 

    app.use(express.bodyParser()); 
    app.set('views',__dirname + '/views'); 
    app.set('view engine', 'ejs'); 
    app.use(express.static(__dirname + '/public')); 
    app.use(express.cookieParser()); 
    app.use(app.router); 

}); 

app.get('/', function (req, res){ 

    res.render('ajax.ejs'); 

}); 

app.post('/ajax', express.bodyParser(), function (req, res){ 

    console.log(req); 
    console.log('req received'); 
    res.redirect('/'); 

}); 

और यह ajax.ejs है:

<html> 
<head> 
    <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>  
    <script type="text/javascript"> 

     $('#enter').click(function(){ 

    $.ajax({ 
      url: '/ajax', 
      type: 'POST', 
      cache: false, 
      data: { field1: 1, field2: 2 }, 
      success: function(data){ 
       alert('Success!') 
      } 
      , error: function(jqXHR, textStatus, err){ 
       alert('text status '+textStatus+', err '+err) 
      } 
     }) 
    });    

</script> 
</head> 
<body> 

<form> 
    <input type="button" id="enter" value="Enter"> 
</form> 

</body> 
</html> 

जब ajax.ejs भरी हुई है, वहाँ कंसोल में किसी भी उत्पादन नहीं है, इसलिए, पोस्ट अनुरोध काम नहीं किया। मैं क्या कर सकता हूँ?

धन्यवाद अग्रिम!

+0

कदम express.bodyParser() app.use करने के लिए() अनुभाग: app.use (express.bodyParser ()); पोस्ट का बॉडी जिसे आप req.body के माध्यम से पुनर्प्राप्त कर सकते हैं। – asgoth

+0

हो गया! इसलिए, मैं 'console.log (req) बदलता हूं;' 'console'log (req.body);'। लेकिन पोस्ट अनुरोध अभी भी काम नहीं कर रहा है, मेरे पास कंसोल में कोई आउटपुट नहीं है। – MrMangado

उत्तर

6

मुझे आपकी समस्या मिली। आपको अपनी स्क्रिप्ट को अपने सिर से अपने शरीर (फॉर्म टैग के बाद) में स्थानांतरित करने की आवश्यकता है:

... 
</form> 
<script type="text/javascript"> 

    $('#enter').click(function(){ 
    ... 

</script> 
</body> 
+0

सच है, यह काम करता है! : डी – MrMangado

1

नीचे दिए गए कोड नए रिलीज़ के अनुसार काम करते हैं।

server.js

var express = require('express') 
var app = express(); 
var bodyparser = require('body-parser'); 
var urlencodedparser = bodyparser.urlencoded({extended:false}) 

app.set('views',__dirname + '/views'); 
app.set('view engine', 'ejs'); 
app.use(express.static(__dirname + '/public')); 
app.use(express.cookieParser()); 

app.get('/', function (req, res){ 
    res.render('ajax.ejs'); 
}); 

app.post('/ajax', urlencodedparser, function (req, res){ 
    console.log(req); 
    console.log('req received'); 
    res.redirect('/'); 

}); 

app.listen(8888); 

ajax.ejs

<html> 
<head> 
    <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>  

</head> 
<body> 

<form> 
    <input type="button" id="enter" value="Enter"> 
    <script type="text/javascript"> 

     $('#enter').click(function(){ 

    $.ajax({ 
      url: '/ajax', 
      type: 'POST', 
      cache: false, 
      data: { field1: 1, field2: 2 }, 
      success: function(data){ 
       alert('Success!') 
      } 
      , error: function(jqXHR, textStatus, err){ 
       alert('text status '+textStatus+', err '+err) 
      } 
     }) 
    });    

</script> 
</form> 

</body> 
</html>