返回成功或错误的简单 Node.js 服务器

发布于 2024-11-29 18:31:59 字数 2297 浏览 1 评论 0原文

这应该很简单,但我似乎无法让它按我的预期工作。

我只希望客户端提交带有电子邮件地址的表单,node.js 服务器在查询提交的电子邮件地址后查找数据库并返回成功或错误。作为第一步,我要做的就是手动返回成功或错误。

在客户端,我使用 JQuery 和 ajaxForm 插件。
在服务器端,我在 node.js 端使用 connect-form 。

我可以从 node.js 端看到 console.log 消息,但在客户端没有看到“SUCCESS”警报。相反,我在 localhost:8000 处看到一个空页面。我认为解决办法很简单。

任何帮助将不胜感激!

客户端 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">
    $(document).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 端:

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 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">
    $(document).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....');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羞稚 2024-12-06 18:31:59

这可能是 Nodejs 返回的内容存在问题。尝试将 ajax 函数更改为 console.log 来自 ajax 调用的响应并查看返回的内容。或者检查响应标头和正文,例如 chrome 的网络监视器

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文