2014-09-11 4 views
5

Mein Kunde macht einen Ajax-AufrufNodeJS Wie Daten in Server erhalten, von Jquery Ajax-Aufruf via POST gesendet

{{ 


     function callNode(){ 

     console.log("I am called"); 
     var data = {"emailId":"[email protected]"}; 



     $.ajax({ 
      type: 'POST', 
      data: JSON.stringify(data), 
      /* data: { 
       blob: {wob:"1",job:"2", ar:[1,2,{a:'b'}]} 
      },*/ 
      contentType: "application/javascript", 
      //contentType: "application/x-www-form-urlencoded", 
      dataType:'json', 
      url: 'http://localhost:3000/notification',      
      success: function(data) { 
       console.log('success'); 
       console.log(JSON.stringify(data));        
      }, 
      error: function(error) { 
       console.log("some error in fetching the notifications"); 
      } 

     }); 
    } 
}} 

ich in der Lage bin Anfrage in meinem app.js zu bekommen, aber nicht in der Lage das bekommen Daten, die ich bin vorbei ich versuchte

{{ 

     app.post('/notification', function(req, res) { 
     JSON.stringify(req.params); 


     /* 
     var body; 
     req.on('data', function(chunk) { 
     console.log("Received body data:");  
     body += chunk; 
     }); 

     // the end event tells you that you have entire body 
     /*req.on('end', function() { 
     try { 
     var data = JSON.parse(body); 
     colnosole.log(data); 

    } catch (er) { 
     // uh oh! bad json! 
     res.statusCode = 400; 
     return res.end('error: ' + er.message); 
    } 

    } 
    */ 


}} 

Sache zu suchen, aber nichts funktioniert wird sein kommen nicht in irgendwelchen Ereignissen von Anfrage und Antwort (wie ich viele Menschen gesehen haben, unter Verwendung der Daten zu holen.

Helfen Sie mir zu wissen, was falsch ist hier sein erstes Mal Ajax am Knoten

Antwort

3

Da Sie Daten als Json Senden muss die content in Bezug auf das geändert werden, so sollte der Ajax-Aufruf sein:

$.ajax({ 
     type: 'POST', 
     data: JSON.stringify(data), 
     /* data: { 
      blob: {wob:"1",job:"2", ar:[1,2,{a:'b'}]} 
     },*/ 
     contentType: "application/json", 
     //contentType: "application/x-www-form-urlencoded", 
     dataType:'json', 
     url: 'http://localhost:3000/notification',      
     success: function(data) { 
      console.log('success'); 
      console.log(JSON.stringify(data));        
     }, 
     error: function(error) { 
      console.log("some error in fetching the notifications"); 
     } 

    }); 

Hierher Sie die content auf application/json geändert sehen können.

Am Serverende müssen Sie den request.body überprüfen, um die Daten zu erhalten und nicht request.params.

+1

Es funktionierte dank V31. Nur die von dir erwähnten Änderungen sind nötig um es laufen zu lassen :) –

1

req.params tut nicht, was Sie denken, dass es tut.

app.get('/:route', function(req, res) { 
    console.log(req.params.route); 
}); 

/test Besuch würde req.params.route mit test füllen.

Sie suchen nach req.body, die nur mit der body-parser Middleware verwendet werden können.

1

Sie haben Körper-Parser-Middleware (https://www.npmjs.org/package/body-parser) verwenden und Sie müssen erklären, welche badyParser

  • app.use zu verwenden (bodyParser.json()) oder
  • app.use (bodyParser. roh()) oder
  • app.use (bodyParser.text()) oder
  • app.use (bodyParser.urlencoded())

    in Ihrer Haupt-js-Datei. Dann wird req.body Objekt Ihre json/text/raw/urlencoded Daten enthalten.

Verwandte Themen