2013-03-24 64 views
0

我有以下的CoffeeScript:AJAX调用使用jQuery + CoffeeScript中不会产生正确

$(document).ready ->` 
     $.ajax 'http://www.omdbapi.com/?i=tt1285016', 
       type: 'GET' 
       dataType: 'json' 
       error: (jqXHR, textStatus, errorThrown) -> $('body').append "AJAX Error: #{textStatus}"   
       success: (data, textStatus, jqXHR) -> $('body').append "Successful AJAX call: #{data}" 

然而,这genrates下面的JavaScript不正确的看向我:

(function() { 
    $(document).ready(function() { 
    return $.ajax('http://www.omdbapi.com/?i=tt1285016'); 
    }); 

    ({ 
    type: 'GET', 
    dataType: 'html', 
    error: function(jqXHR, textStatus, errorThrown) { 
     return $('body').append("AJAX Error: " + textStatus); 
    }, 
    success: function(data, textStatus, jqXHR) { 
     return $('body').append("Successful AJAX call: " + data); 
    } 
    }); 

}).call(this); 

谁能告诉我哪里出错了?

感谢, 亚当

+1

您的代码在coffeescript.org上正常工作(减去那个') – Ven 2013-03-24 17:50:59

回答

0

我无法编译当前的代码(CoffeeScript version 1.6.1),但如果你$(document).ready ->后删除`象征它编译于

(function() { 

    $(document).ready(function() { 
    return $.ajax('http://www.omdbapi.com/?i=tt1285016', { 
     type: 'GET', 
     dataType: 'json', 
     error: function(jqXHR, textStatus, errorThrown) { 
     return $('body').append("AJAX Error: " + textStatus); 
     }, 
     success: function(data, textStatus, jqXHR) { 
     return $('body').append("Successful AJAX call: " + data); 
     } 
    }); 
    }); 

}).call(this); 

我不是确定CoffeeScript如何处理换行符,但我想你正在编译一个文件Windows end-of-linesCRLF)在*nix系统上,并导致问题。

我说,因为类似下面的代码:

$(document).ready -> 

     $.ajax 'http://www.omdbapi.com/?i=tt1285016', 

     type: 'GET' 

     dataType: 'html' 

     error: (jqXHR, textStatus, errorThrown) -> $('body').append "AJAX Error: #{textStatus}" 

     success: (data, textStatus, jqXHR) -> $('body').append "Successful AJAX call: #{data}" 

编译成一个你已经发布。 (请注意,在,之后的第3行有一个换行符)

+0

谢谢,最终成为我的文本编辑器的问题, – adamjmarkham 2013-03-24 18:14:35

相关问题