【发布时间】: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 选择器中缺少 $。
-
现在我添加了它,它在等我,点击确定后它会刷新