语法 类和对象 字符串 数组 日期和时间 数学 方法 元编程 jQuery Ajax 正则表达式 网络 设计模式 数据库 测试

发布于 2016-05-29 08:17:40 | 112 次阅读 | 评论: 0 | 来源: 网络整理

问题

你想要使用 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

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务