Javascript 解析AJAX响应

Javascript 解析AJAX响应,javascript,jquery,ajax,coffeescript,lint,Javascript,Jquery,Ajax,Coffeescript,Lint,我想对所有帮助我的人说声谢谢。但我无法正确解析AJAX响应: 我的AJAX请求: $('#sumbit_LoggingGet').on 'click', -> username = $('#login_username').val() password = $('#login_password').val() mac_id = $('#login_LoggingGetmac').val() id = $('#login_LoggingGetid').val() $.ajax type

我想对所有帮助我的人说声谢谢。但我无法正确解析AJAX响应:

我的AJAX请求:

$('#sumbit_LoggingGet').on 'click', ->
username = $('#login_username').val()
password = $('#login_password').val()
mac_id = $('#login_LoggingGetmac').val()
id = $('#login_LoggingGetid').val()

$.ajax
  type: "GET"
  url: start_url + mac_id + "/log-config/" + id
  dataType: "json"
  crossDomain: true
  cache: false
  beforeSend: beforeSend(username, password)

  success: (data) ->
    console.dir data
    successMessage("""<h1>Logging Get Results</h1>""")
    clearColor(areaText = '#header_username')
    clearColor(areaText = '#header_password')
    clearColor(areaText = '#header_LoggingGetmac')
    clearColor(areaText = '#header_LoggingGetid')

    for key,value of data
      $('#data-results').append """<br>
      <h3><span style="color: #0000CD;"> #{key}</span></h3>
      <br><h4> #{value} #{value.id}</h4><br>"""

它工作正常。您的
undefined
是准确的,因为您的值都不是具有
id
属性的
对象。所有值都是
数组
字符串

jQuery中的
append
方法在当前上下文中不进行模板标记替换。它只需要一个HTML字符串、另一个jQuery集合或一个DOM节点

您将需要执行以下操作:

var html = "...".replace(/#\{(\w+)\}/g, function(match, tag) {
    return data[tag] || "";
});

$(...).append(html);

根据评论编辑答案

JSON.stringify
是您需要的。stringify采用一个参数进行打印:

$.ajax
键入:“获取”
url:start\u url+mac\u id+“/log config/”+id
数据类型:“json”
跨域:对
缓存:false
发送前:发送前(用户名、密码)
成功:(数据)->
console.dir数据
成功消息(“记录获取结果”)
clearColor(areaText='#标题(u用户名))
clearColor(areaText='#标题(U密码))
clearColor(区域文本='#标题_LoggingGetmac')
clearColor(areaText='#标题_LoggingGetid')
$(“#数据结果”).html JSON.stringify(数据,未定义,2)

@MikeW我认为三重引号是一种咖啡脚本。如果您不熟悉这种语法,至少可以通过javascript转换器运行咖啡脚本。如果您这样认为,那就太糟糕了,我的答案是正确的。@GregBurghardt,您的答案可能在某个地方正确,但不在这个代码块中。请使用
{value[0]?.id}
而不是
{value.id}
谢谢!但是我的结果现在是这样的,你仍然输出
{value}
?对于你在问题中提供的输入数据,我觉得这是正确的。你期望的结果是什么?我正试图让它与原始帖子中的真实结果相匹配。
var html = "...".replace(/#\{(\w+)\}/g, function(match, tag) {
    return data[tag] || "";
});

$(...).append(html);
$.ajax
  type: "GET"
  url: start_url + mac_id + "/log-config/" + id
  dataType: "json"
  crossDomain: true
  cache: false
  beforeSend: beforeSend(username, password)

  success: (data) ->
    console.dir data
    successMessage("""<h1>Logging Get Results</h1>""")
    clearColor(areaText = '#header_username')
    clearColor(areaText = '#header_password')
    clearColor(areaText = '#header_LoggingGetmac')
    clearColor(areaText = '#header_LoggingGetid')
    $('#data-results').html JSON.stringify(data,undefined,2)