May 09, 2021 CoffeeScript
You want to use jQuery to call 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}"
Both jQuery 1.5 and the newer version have added a new and complementary API to handle different callbacks.
request = $.get '/'
request.success (data) -> $('body').append "Successfully got the page again."
request.error (jqXHR, textStatus, errorThrown) -> $('body').append "AJAX Error: ${textStatus}."
The jQuery and $variables are interchangeable. See also Callback bindings.