使用coffeescript对Ajax响应的空值

时间:2017-08-26 22:02:57

标签: jquery ruby-on-rails ajax coffeescript

我在咖啡脚本中得到了ajax - 它将值发送到后端。在浏览器面板中,我可以看到内部有json的响应,但是在成功的情况下 - 所有变量都是未定义的。有人可以帮忙吗?

这里是ajax的代码。

data, textStatus, jxQhr

所有变量public class AssetDao implements Dao<Asset>{ public static AssetDao instance; private String db ="deterioro"; private String assetsModule = "import module namespace assets='http://creativosdigitales.co/cartera/assets' at 'C:/Users/santiago umaña/Google Drive/Deterioro/xquery/assets.xqm';"; private String simulationsModule = "import module namespace simulations='http://creativosdigitales.co/cartera/simulations' at 'C:/Users/santiago umaña/Google Drive/Deterioro/xquery/simulations.xqm';"; public static AssetDao getInstance() { if(instance == null) { instance = new AssetDao(); } return instance; } @Override public List<Asset> get() throws IOException, JAXBException { List<Asset> results = new ArrayList<Asset>(); try(BaseXClient session = new BaseXClient("localhost", 1984, "admin", "admin")){ session.execute("open "+db); final String xquery = "for $data in subsequence(//asset, 1, 20) return <asset> {$data/assetid} {$data/payment} {$data/pendingpayments} <balance>{ format-number($data/balance, \"#.00\") }</balance> <npv>{ format-number(sum(//simulation[assetid=$data/assetid]/payment/npv), \"#.00\") }</npv> <difference>{ format-number($data/balance - sum(//simulation[assetid=$data/assetid]/payment/npv), \"#.00\") }</difference> </asset>"; JAXBContext context = JAXBContext.newInstance(Asset.class); Unmarshaller unmarshaller = context.createUnmarshaller(); Query query = session.query(xquery); while(query.more()) { results.add((Asset) unmarshaller.unmarshal(new StringReader(query.next()))); } return results; } } //custom methods public List<ExtraField> getExtraFields(String assetId) throws IOException, JAXBException { List<ExtraField> extraFields = new ArrayList<ExtraField>(); try(BaseXClient session = new BaseXClient("localhost", 1984, "admin", "admin")){ final String xquery = assetsModule + "let $db := collection('"+db+"')" + "let $asset := assets:asset-info($db, '"+assetId+"')" + "return assets:extra-fields($asset)"; JAXBContext context = JAXBContext.newInstance(ExtraField.class); Unmarshaller unmarshaller = context.createUnmarshaller(); Query query = session.query(xquery); System.out.println(query.toString()); while(query.more()) { extraFields.add((ExtraField) unmarshaller.unmarshal(new StringReader(query.next()))); } return extraFields; } } public List<Simulation> getSimulation(String assetId) throws IOException, JAXBException { List<Simulation> simulations = new ArrayList<Simulation>(); try(BaseXClient session = new BaseXClient("localhost", 1984, "admin", "admin")){ final String xquery = assetsModule + simulationsModule + "let $db := collection('"+db+"')" + "let $asset := assets:asset-info($db, '"+assetId+"')" + "return simulations:simulation-xml($asset)"; JAXBContext context = JAXBContext.newInstance(Simulation.class); Unmarshaller unmarshaller = context.createUnmarshaller(); Query query = session.query(xquery); System.out.println(query.toString()); while(query.more()) { simulations.add((Simulation) unmarshaller.unmarshal(new StringReader(query.next()))); } return simulations; } } 都是未定义的。我如何从这些变量中获取这些值?

1 个答案:

答案 0 :(得分:1)

你应该查看你正在尝试做的文档:

$.post

$.ajax

这是执行帖子的两种方式,但它们有不同的方法签名。

就像在评论中所说,你在这里有的是不同语法的混合,这两种语法都无效。

在大多数情况下,它看起来像$.ajax签名,所以你可以稍微改变它(注意我还修复了缩进 - 尝试在问题中发布代码时正确,因为它是对于像coffeescript这样的语言很重要:

# Note i'm using string interpolation, not concatenation
$.ajax "/articles/#{id}/comments",
  # add this key-val to determine the request type
  method: "POST"
  contentType: 'application/json'
  data: comment_params:
    commenter: commenter
    body: body
  success: (data, textStatus, jQxhr) ->
    console.log(textStatus)
    $('#comments').append JSON.stringify(data)
  dataType: 'json'