weixin_33701251 2014-10-30 15:17 采纳率: 0%
浏览 51

Ajax调用和node.js

I have a problem integrating ajax calls with node.js (I am using express.js). This is my code:

Page.html

<div id = "prezzo" class="col-md-12" style = "display : none">
      <div class="col-md-2 col-md-offset-3">
        <h1 id = "h1">€ </h1>
      </div>
      <div class="col-md-4">
        <input style="margin-top: 17px" type="submit" class="btn btn-sommatinese btn-lg btn-block" />
      </div>
    </div>

    </div>
<script type = "text/javascript">

$("#form1").submit(function()
            {
              $("#prezzo").fadeIn(600, function()
                {
                  $.ajax({
                    url: "http://localhost:3000/prezzo",
                    type: "GET",

                    success: function(data)
                    {
                      console.log(data);
                      $("#h1").append(data.biglietto);
                    }
                  });
                });
            });
</script>

Page.js

exports.prezzo = function(req,res) {
req.getConnection(function(err,connection) {
    connection.query("my query", function(err,rows) {

      if(err) console.log("Error selecting : %s", err);

      if(rows.length > 0) {

          totale = rows[0];
          res.send(totale);
          //console.log(totale);
        }

    });
});
}

The ajax call doesn't work, what I have is a white page with the result of my query, like this: { biglietto: 2.70 }

  • 写回答

1条回答 默认 最新

  • weixin_33743880 2014-10-30 16:02
    关注

    I can't see #form1 in your HTML, but anyway ...

    The problem is that when you fire a submit event, a whole bunch of default behavior occurs in addition to what you put in your submit handler, such as repainting the whole page with the results of the request (which you don't want).

    What you need to do is prevent the default behavior, so that your event handler is the only thing that happens on submit. The first two lines of your submit handler should look like this:

    $("#form1").submit(function(event){
        event.preventDefault();
    

    Then you can continue on with the rest of your code.

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?