为什么在向Express服务器发送数据时,我的$ .get()回调函数从未被调用过?

时间:2015-11-19 15:56:24

标签: javascript jquery express

有人可以告诉我为什么我的回调功能:

$.get("http://localhost:8080/", msg, function(data) {
                        alert("Data" + data);
                    });

永远不会被召唤?

服务器获取请求并打印到控制台,但我的客户端(下面的HTML页面)似乎从未得到响应。

这是Express服务器:

var express = require('express');
var app = express();

app.get('/', function(req, res) {
    console.log("Message! : GET");
    res.send('Hello World');
});

app.post('/', function(req, res) {
    console.log("Message! : POST");
    res.send('Hello World');
});

var server = app.listen(8080, function() {
    var host = server.address().address;
    var port = server.address().port;
    console.log('Listening at http://%s:%s', host, port);
});

以下是向服务器发出HTML页面请求:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Client</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    </head>
    <body>
        <h1>Client</h1>

        <form action="">
            <fieldset>
                Name:<br><br>
                <input type="text" id="name" name="name">
                <br><br>
                Message:<br><br>
                <input type="text" id="message" name="message">
                <br><br>
                <input type="submit" id="submit" value"Submit">
            </fieldset>
        </form>
        <br><br>

        <textarea style="max-height:400px; min-height:400px; max-width:300; min-width:300px; resize: none"></textarea>

        </textarea>

    <script type="text/javascript">
        document.getElementById("submit").addEventListener("click", function(e) {
            var msg = {
                        Name: document.getElementById("name").value,
                        Message: document.getElementById("message").value
                    };


                    $.get("http://localhost:8080/", msg, function(data) {
                        alert("Data" + data);
                    });

        }, false);
    </script>

    </body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试使用下面的代码来查看您收到的错误消息(作为代码可靠性的响应发布)

@IBAction func valueForItemChanged(sender: AnyObject) {
print("value changed");
dispatch_async(dispatch_get_main_queue(), {
    self.bimButtonBadge.text = String(self.getTotalItemQuantity())
    self.bimButtonBadge.transform = CGAffineTransformMakeScale(0.2, 0.2)
    UIView.animateWithDuration(1.0,
        delay: 0,
        usingSpringWithDamping: 0.2,
        initialSpringVelocity: 4.0,
        options: UIViewAnimationOptions.AllowUserInteraction,
        animations: {
            self.bimButtonBadge.transform = CGAffineTransformIdentity
        }, completion: nil)
});