Crafty.js使用CoffeeScript未显示正确响应

Crafty.js not showing correct response using CoffeeScript

本文关键字:响应 显示 js 使用 CoffeeScript Crafty      更新时间:2023-09-26

我使用的是Crafty.js,我最近添加了一个用于显示帮助信息的按钮,该按钮运行良好,并且我正确地从服务器获得了返回,但我不断收到错误。

要解释的时间很长,所以要耐心。

这是我的代码:

where = this._current
auxiliary = $.getJSON('/help/', {'scene': where})
Crafty.e("HTML")
.attr({x: 100, y: 200, w: 224, h: 200})
.replace """
  <font color="white">
    #{auxiliary.message}
  </font>
   """

当代码是这样的时候,它显示的是:undefined,但是,如果我把代码改成:

Crafty.e("HTML")
.attr({x: 100, y: 200, w: 824, h: 400})
.replace """
  <font color="white">
    #{auxiliary}
  </font>
   """

它显示的是:[object Object]

服务器返回的数据如下:

{
 "message":
   "<p>Help text</p>",
 "result":
   "ok"
}

我错过了什么?

$.getJSON是异步的,只会返回一个deferred,它没有message属性。您应该将代码附加到回调:

where = this._current
$.getJSON('/help/', {'scene': where}).done (response) ->
  Crafty.e("HTML")
  .attr({x: 100, y: 200, w: 224, h: 200})
  .replace """
    <font color="white">
      #{response.message}
    </font>
  """