【问题标题】:coffescript and jquery ajax call [closed]coffeescript 和 jquery ajax 调用 [关闭]
【发布时间】:2011-10-13 23:15:01
【问题描述】:

我正在尝试使用 coffescript 和 jquery 进行 ajax 调用并使用结果更新表单输入类型,但我的输入使用 [object XMLDocument] 进行更新,而不是返回文本

这是我使用的咖啡脚本代码。

$ ->
 $('#get-mac').live 'click', (e) =>
     e.preventDefault()
     podaci = {broj : $('#contract_no').val(), action : 'get-mac-ua'}
     $.ajax '/hhh'
         type: 'POST'
         data: podaci
         datatype: 'text'
         success: (data) ->
             if data == 'False'
                 $('#mac').removeAttr "readonly"
                 alert 'Ne postoji MAC adresa na UA, upiši ručno'
             else
                 $('#mac').val data
                 $('#mac').removeAttr "readonly"
                 $('#contract_no').attr "readonly", true

这是旧的 js 版本,可以工作

$(document).ready(function(){
$("#get-mac").live('click', function(e){
    e.preventDefault();
    var podaci = {broj : $('#contract_no').val(), action : 'get-mac-ua'};
    $.ajax({
      type: "POST",
      url: '/hhh',
      data: podaci,
      dataType: 'html',
      success: function(data){
          if(data == "False")
              {
                  $('#mac').removeAttr("readonly");
                  alert('Ne postoji MAC adresa na UA, upiši ručno');
              }
          else
              {
                  $('#mac').val(data);
                  $('#mac').removeAttr("readonly");
                  $('#contract_no').attr("readonly", true)
              }
        }
    });
  });
});

【问题讨论】:

  • 执行console.log(data); 并在firebug 控制台中查看您会得到什么,您必须执行data.property
  • console.log(data); ReferenceError:数据未定义
  • 你考虑过将dataType改为html,dataType:"html"
  • 与 dataType:"html" 相同

标签: jquery ajax coffeescript


【解决方案1】:

您的 JS 代码和 CoffeeScript 之间的显着区别只是您将 dataType 更改为 datatype。资本化问题! :)

【讨论】:

    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    相关资源
    最近更新 更多