CoffeeScript - AJAX Post/Get 问题,返回空白结果
我正在用 Coffeescript 重写一个网站,并且遇到了一些非常低级的错误。我知道这可能是一个非常简单的答案,但我已经坚持这个问题很长一段时间了,所以决定发布。
这是我的代码:
$ ->
rankings = new Rankings
$('.filter').click ->
#highlighting filter on click
if $(this).hasClass('genre-filter')
rankings.filters.set('genre', $(this).html().toLowerCase())
else
rankings.filters.set('time', $(this).html().toLowerCase())
#todo: add loading screen to rankings here
#ajax post
$.post '../history/ajax/rankingsajax.php',
genrefilter: rankings.filt('genre')
timefilter: rankings.filt('time')
artistfilter: rankings.filt('artist')
userfilter: rankings.filt('user')
(data) ->
alert data
无论我如何尝试摆弄它,它总是最终会警告一个空白的弹出窗口。所以我想尝试一个更简单的例子,只使用 get 请求。我尝试了以下操作:
$.get '../index.html', (data) -> alert data
但是,这也会导致弹出空白窗口。
上面的代码正确编译为 JS,所以我不知道从哪里开始。任何帮助表示赞赏。
-卡尔文
I am in the process of rewriting a website in Coffeescript and I am having trouble with some very nooby errors. I know it is probably a very simple answer, but I've been stuck on this for quite some time so decided to post.
Here is my code:
$ ->
rankings = new Rankings
$('.filter').click ->
#highlighting filter on click
if $(this).hasClass('genre-filter')
rankings.filters.set('genre', $(this).html().toLowerCase())
else
rankings.filters.set('time', $(this).html().toLowerCase())
#todo: add loading screen to rankings here
#ajax post
$.post '../history/ajax/rankingsajax.php',
genrefilter: rankings.filt('genre')
timefilter: rankings.filt('time')
artistfilter: rankings.filt('artist')
userfilter: rankings.filt('user')
(data) ->
alert data
No matter how I try and fiddle with it, it always ends up alerting a blank pop-up window. So I thought I'd try a simpler example and just use a get request. I tried the following:
$.get '../index.html', (data) -> alert data
However, this also results in a blank pop-up window.
The the above code compiles correctly to JS so I am lost as to where to begin. Any help is appreciated.
-Calvin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要害怕使用花括号和圆括号。它们不是必需的,但它们可以帮助澄清正在发生的事情。
空白的弹出窗口至少让您知道您正在获取成功回调,这很好。因此,也许检查一下您调用的网址是否正确。我注意到您使用的是相对路径
../history/ajax/rankingsajax.php
,请确保该路径相对于使用 JavaScript 的 HTML 页面,而不是相对于 JavaScript 文件本身。我将使用
console.log(data)
代替alert(data)
,并使用 Firebug 或 Chrome 和 Safari 中的 JS 控制台来查看发生了什么。控制台提供的信息比警报更多。祝你好运,
桑德罗
Don't be afraid to use curly braces and parenthesis. They're not required, but they can help clarify what's going on.
The blank popup at least lets you know you're getting to the success callback, which is good. So maybe check that you're calling the correct url. I noticed you're using a relative path,
../history/ajax/rankingsajax.php
, make sure that path is relative to the HTML page that is using the JavaScript and not the JavaScript file itself.Instead of
alert(data)
, I would useconsole.log(data)
and use Firebug or the JS console in Chrome and Safari to see what's going on. The console provides more information than an alert.Good luck,
Sandro
我不确定你的相对网址与后退。尝试扩展路径。
I'm not sure about your relative URLs with backsteps. Try to expand path.