开发者

Simple node.js server to return success or error

This is should be pretty simple, but I can't seem to get it to work as I intended.

I just want a client submit a form with an email address, and node.js server looks up db and return success or error after querying for the submitted email address. As the first step, all I am trying to do is to manually return success or error.

On client side, I am using JQuery and ajaxForm plugin.

On server side I am using connect-form on node.js side.

I can see console.log message from node.js side, but I don't see "SUCCESS" alert on the client side. Instead I see an empty page at localhost:8000. I think the fix would be something simple.

Any help would be much appreciated!!!!

Client-side HTML/JS:

<div class="gform">
    <form id="entryForm" action="http://localhost:8000/" method="post">
        <p><input type="radio" name="choice" value="1"> Choice 1</p>
        <p><input type="radio" name="choice" value="2"> Choice 2</p>
        <p><input type="radio" name="choice" value="3"> Choice 3</p>
        <p><input type="radio" name="choice" value="4"> Choice 4</p>
        <p><input type="radio" name="choice" value="5"> Choice 5</p>
        <p><input type="text" name="email"> Email Address</p>
        <p><input type="submit" value="Submit" /></p>
    </form>
</div>
<div id="form_result">
</div>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">
    $(doc开发者_开发技巧ument).ready(function() {
        var options = {
            dataType: 'json',
            target: "form_result",
            success: goodResult,
            error: badResult,
            timeout: 1000 
        };

        $('#entryForm').ajaxForm(options);
    }

    function goodResult() {
        alert('SUCCESS!');
    }

    function badResult() {
        alert('ERROR!');
    }
</script>

Node.js side:

var form = require('connect-form'), connect = require('connect');
var server = connect.createServer(
  form({ keepExtensions: true }),
  function(req, res){
    // Form was submitted
    if (req.form) {
      req.form.complete(function(err, fields){
        console.log(fields);
        res.writeHead(200, { "Content-Type": "application/json" });
        res.end();
      });
    } else {
      // Regular request, return error.
    }
  }
);
server.listen(8000);
console.log('Express app started on port 8000....');


This could be a issue with what's being returned by nodejs. Try changing your ajax function to console.log the response from the ajax call and see what is being returned. That or check the response headers and body in something like chrome's network monitor

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜