从EX preSS JS阿贾克斯后响应不断抛出错误 [英] Ajax post response from express js keeps throwing error

查看:137
本文介绍了从EX preSS JS阿贾克斯后响应不断抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个真正的困难时期站起来从一个html页面的双向数据传输到我的Node.js应用程序,然后返回HTML页面。

I am having a real hard time standing up bidirectional data transfer from a html page to my node.js application and then back to the html page.

我是pretty的肯定,我遍了正确的解决方案,但我只是没有得到它的工作。

I'm pretty sure I'm all over the correct solution, but I'm just not getting it to work.

我用以下为我的node.js应用程序:

I'm using the following for my 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 is up and running");  });

以下是我的html页面:

The following is my html page:

<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 

    <script type="text/javascript"> 
        // wait for the DOM to be loaded 
        $(document).ready(function() 
        {
            alert("Got here!");

            function DisplayName(response)
            {
                alert(response.newName);
            }

            $("#NameGameIt").click(function(event)
            {
                alert("How about 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 for Ajax</h1>
</br>

Name: <input type="text" id="name" name="name"></br>
<input type="button" value="NameGameIt" id="NameGameIt">
</form>

</body>
</html>

所以,当我运行的节点应用,服务器上来就好了。当我火了HTML页面,我得到警报在这里得到!当我preSS按钮NameGameIt,我得到警报现在怎么样?其次是警示姓名:+什么名字,我输入。当我点击关闭该警告,该节点的应用程序立即看到后,并打印出了名到控制台。两秒钟后,当节点发送响应时,浏览器会正确插入阿贾克斯后的错误处理程序。的唯一有用的数据出来的错误处理程序是,它是一个错误,这是不是真的有用。

So, when I run the node application, the server comes up just fine. When I fire up the html page, I get the alert "Got here!" When I press the button "NameGameIt", I get the alert "How about now?" followed by the alert "Name: " + whatever name I entered. Once I click off that alert, the node application immediately sees the post and prints out the name to the console. Two seconds later when the node sends the response, the browser will go right into the error handler on the ajax post. The only useful data to come out in the error handler is that it is an "error" which isn't really useful.

所以我知道该消息得到从HTML页面的节点,因为它打印出来,我发出的名字,我知道的HTML页面从节点收到消息回来,因为它不会出错,直到超时在节点上发生的触发的发送。但是,我不知道为什么它不断给我的错误处理程序,而不是成功。我已经通过在节点code一路使用节点检查阶梯,它似乎正确地构建数据包以200 code的成功和它调用.END内后。发送它的完成使数据包,所以我不认为任何的那些东西咬我。

So I know the message is getting to the node from the html page because it prints out the name that I sent and I know the html page is getting the message back from the node because it won't error out until the timeout on the node happens triggering the send. But, I have no idea why it keeps on sending me to the error handler instead of the success. I've stepped through all the way in the node code using node-inspector and it seems to be building the packet correctly with a 200 code for success and it calls .end inside .send after it's done making the packet so I don't think either of those things are biting me.

我快要发疯!如果有人认为我缺少什么,或者有办法收集更多的信息什么新的想法,我将非常感谢您的帮助。

I'm about to go nuts! If anyone sees what I'm missing or has any new ideas on ways to gather more information, I would be very grateful for the help.

推荐答案

您code是完全正常的,但你几乎可以肯定,运行到一个跨域Ajax请求的问题。你可能会在本地文件系统中打开该HTML文件,并发出请求的方式,这是什么原因造成了这个问题。

Your code is perfectly fine, but you're almost certainly running into a cross-domain AJAX request issue. You might be opening this HTML file on the local filesystem and making requests that way, which is what is causing this problem.

要解决这个问题,添加 app.use(如press.static(公共)); 像这样:

To fix it, add app.use(express.static('public')); like 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 is up and running");  });

,然后将你的HTML文件中的公共文件夹中。一旦你启动服务器,您可以访问 http://127.0.0.1:8088/file.html 和你的code将会运行得很好。

and then place your html file in the 'public' folder. Once you launch your server, you can visit http://127.0.0.1:8088/file.html and your code will run fine.

这篇关于从EX preSS JS阿贾克斯后响应不断抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆