Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

coffeescript with jquery ajax

Tags:

coffeescript

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

there is something wrong with the above code,I can't compile it into js

like image 907
magicshui Avatar asked Jun 19 '11 05:06

magicshui


1 Answers

The compiler gives the error

Parse error on line 3: Unexpected 'IDENTIFIER'

referring to the line

dataType: 'html' error: (jqXHR, textStatus, errorThrown) ->

The problem is simply that there's no comma (or line break) between 'html' and error. Here's the fixed code:

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

I highly recommend using an editor with a built-in "Build" command, especially one that can work on selected text. It makes syntax errors a lot easier to pin down.

like image 177
Trevor Burnham Avatar answered Oct 20 '22 15:10

Trevor Burnham