weixin_33704591 2014-07-11 13:45 采纳率: 0%
浏览 38

Coffeescript Ajax失败

I get a coffeescript like this

$(document).ready ->
  $('.rpt-date > a').click ->
    projectName = $(this).parent().parent().prev().text()
    date = $(this).text()
    urlStr = '/monitor/show?project=' + projectName + '&date=' + date
    alert(urlStr)
    # $.get urlStr, (data) ->
    #   $('#right-column').append("ajax success")

and it compile to js as following

(function() {
  $(document).ready(function() {
    return $('.rpt-date > a').click(function() {
      var date, projectName, urlStr;
      projectName = $(this).parent().parent().prev().text();
      date = $(this).text();
      urlStr = '/monitor/show?project=' + projectName + '&date=' + date;
      return alert(urlStr);
    });
  });

}).call(this);

Everything works fine. When I click '.rpt-data > a' tag, there's an alert window popup.

But when I change the coffeescript a little (comment alert, uncomment $.get)

$(document).ready ->
  $('.rpt-date > a').click ->
    projectName = $(this).parent().parent().prev().text()
    date = $(this).text()
    urlStr = '/monitor/show?project=' + projectName + '&date=' + date
    #alert(urlStr)
    $.get urlStr, (data) ->
      $('#right-column').append("ajax success")

It compile to js as following

(function() {
  $(document).ready(function() {
    return $('.rpt-date > a').click(function() {
      var date, projectName, urlStr;
      projectName = $(this).parent().parent().prev().text();
      date = $(this).text();
      urlStr = '/monitor/show?project=' + projectName + '&date=' + date;
      return $.get(urlStr, function(data) {
        return $('#right-column').append("ajax success");
      });
    });
  });

}).call(this);

It failed to trigger http request to '/monitor/show?xxxxxxx'.

I use chrome developer tool to trace the code and found out return $.get(urlStr, function(data) {... doesn't execute. It was jumpped over.

I've googled a lot. Some says windows end of line issue, and some says spaces or tabs issue. I tried both but it still didn't work.

Any ideas ? Thanks!

  • 写回答

0条回答 默认 最新

    报告相同问题?