CoffeeScript - AJAX Post/Get 问题,返回空白结果

发布于 2024-12-26 16:58:17 字数 1032 浏览 1 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青朷 2025-01-02 16:58:18

不要害怕使用花括号和圆括号。它们不是必需的,但它们可以帮助澄清正在发生的事情。

$ ->
    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) -> console.log(data)

空白的弹出窗口至少让您知道您正在获取成功回调,这很好。因此,也许检查一下您调用的网址是否正确。我注意到您使用的是相对路径 ../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.

$ ->
    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) -> console.log(data)

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 use console.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

心是晴朗的。 2025-01-02 16:58:18

我不确定你的相对网址与后退。尝试扩展路径。

I'm not sure about your relative URLs with backsteps. Try to expand path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文