CoffeeScript AJAX

AJAX

问题

你想要使用jQuery来调用AJAX。

解决方案

$ ?= require jquery # 由于 Node.js 的兼容性

$(document).ready ->
    # 基本示例
    $.get /, (data) ->
        $(body).append "Successfully got the page."

    $.post /,
        userName: John Doe
        favoriteFlavor: Mint
        (data) -> $(body).append "Successfully posted to the page."

    # 高级设置
    $.ajax /,
        type: GET
        dataType: html
        error: (jqXHR, textStatus, errorThrown) ->
            $(body).append "AJAX Error: #{textStatus}"
        success: (data, textStatus, jqXHR) ->
            $(body).append "Successful AJAX call: #{data}"

jQuery 1.5和更新版本都增加了一种新补充的API ,用于处理不同的回调。

request = $.get /
    request.success (data) -> $(body).append "Successfully got the page again."
    request.error (jqXHR, textStatus, errorThrown) -> $(body).append "AJAX Error: ${textStatus}."

讨论

其中的jQuery和$变量可以互换使用。另请参阅Callback bindings

作者:terry,如若转载,请注明出处:https://www.web176.com/coffeescript/10636.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年2月26日
下一篇 2023年2月26日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注