【问题标题】:Get json from the server without refreshing the page?从服务器获取json而不刷新页面?
【发布时间】:2015-03-24 18:59:01
【问题描述】:

我尝试向服务器发出最少的请求,这是通过向客户端发送一个 json 文件,然后处理这个 json 文件,

但是,如何避免页面被刷新?我希望在不刷新页面的情况下收到json

这是我的 js/python 文件:

$( document ).ready(function() {
    $("#latlon").on("submit", function(){
        $.ajax({
        type: "post",
        url: "/latlon",
        data: ("#latlon").serialize(),
        contentType: "application/x-www-form-urlencoded",
        success: function(responseData) {
            alert(responseData)
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);
        }
        })
    })
    })


class LatLon(MainHandler):
    @tornado.gen.coroutine
    def post(self):
        lat = self.get_argument("lat")
        lon = self.get_argument("lon")
        wwo.request(key=key, q="{},{}".format(lat, lon), format="json", showlocaltime="yes", cc="no", tp=6, extra="isDayTime")
        yield tornado.gen.Task(ioloop.IOLoop.instance().add_timeout, time.time() + 1)
        self.write(json_encode(wwo.result))

我是 Ajax 的新手,每次执行此程序时,我什至都没有收到我的 alert。 如何在不刷新页面的情况下发送块?

注意:有 getJSON 方法,但它需要一些 Ready json

【问题讨论】:

  • 调试你的代码。您的第二个 ("#latlon") jQuery 选择器中缺少 $。
  • 现在我添加了它,它在等我,点击确定后它会刷新

标签: jquery ajax json tornado


【解决方案1】:

您应该在提交事件中添加e.preventDefault(); 以防止页面刷新。

$("#latlon").on("submit", function(e){
 e.preventDefault();
 // code here
})

【讨论】:

    猜你喜欢
    • 2020-09-28
    • 2021-01-09
    • 1970-01-01
    • 2011-04-24
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    相关资源
    最近更新 更多