javascript - Ajax post response from express js keeps throwing error -


i having real hard time standing bidirectional data transfer html page node.js application , html page.

i'm pretty sure i'm on correct solution, i'm not getting work.

i'm using following node.js application:

var express = require('/usr/lib/node_modules/express'); var app = express();  app.use(function(err, req, res, next){   console.error(err.stack);   res.send(500, 'something broke!'); });  app.use(express.bodyparser());  app.post('/namegame', function(req, res)     {         console.log('got request name: ' + req.body.name);         settimeout(function()          {              var newname = "new name-o";             res.send({name: newname});         }, 2000);     } );  app.listen(8088, function() { console.log("server , running");  }); 

the following html page:

<html>  <head>      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>       <script type="text/javascript">          // wait dom loaded          $(document).ready(function()          {             alert("got here!");              function displayname(response)             {                 alert(response.newname);             }              $("#namegameit").click(function(event)             {                 alert("how now?");                 //event.preventdefault();                 var namesubmitted = document.getelementbyid("name");                 //var namesubmitted = "help!!";                  alert("name: " + namesubmitted.value);                  $.ajax({                     type:       "post",                     url:        "http://127.0.0.1:8088/namegame",                     data:       {name: namesubmitted.value},                     datatype:   "json",                     timeout:    2500,                     success:    function() { alert("???");},                     error:      function(error1, error2, error3)                     {   alert("error"); },                     complete:   function(arg1, arg2, arg3)                     {   alert("complete");  }                 });              });          });      </script>  </head>  <body>  <h1>test form ajax</h1> </br>  name: <input type="text" id="name" name="name"></br> <input type="button" value="namegameit" id="namegameit"> </form>  </body> </html> 

so, when run node application, server comes fine. when fire html page, alert "got here!" when press button "namegameit", alert "how now?" followed alert "name: " + whatever name entered. once click off alert, node application sees post , prints out name console. 2 seconds later when node sends response, browser go right error handler on ajax post. useful data come out in error handler is "error" isn't useful.

so know message getting node html page because prints out name sent , know html page getting message node because won't error out until timeout on node happens triggering send. but, have no idea why keeps on sending me error handler instead of success. i've stepped through way in node code using node-inspector , seems building packet correctly 200 code success , calls .end inside .send after it's done making packet don't think either of things biting me.

i'm go nuts! if sees i'm missing or has new ideas on ways gather more information, grateful help.

your code fine, you're running cross-domain ajax request issue. might opening html file on local filesystem , making requests way, causing problem.

to fix it, add app.use(express.static('public')); so:

var express = require('/usr/lib/node_modules/express'); var app = express();  app.use(function(err, req, res, next){   console.error(err.stack);   res.send(500, 'something broke!'); });  app.use(express.bodyparser()); app.use(express.static('public'));  app.post('/namegame', function(req, res)     {         console.log('got request name: ' + req.body.name);         settimeout(function()          {              var newname = "new name-o";             res.send({name: newname});         }, 2000);     } );  app.listen(8088, function() { console.log("server , running");  }); 

and place html file in 'public' folder. once launch server, can visit http://127.0.0.1:8088/file.html , code run fine.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -