有没有一种方法可以将error:
参数添加到此行中而不使用每行格式的单个参数?有没有办法在CoffeeScript中为这个JQuery调用添加另一个函数引用参数,而不用为每个参数使用一行?
$.ajax type:'DELETE', url: '/history', data: {id: id}, success: (data)->
$('#row'+index).detach()
我知道我可以把它变成
$.ajax
type: 'DELETE'
url: '/history'
data: id: id
success: (data) ->
$('#row' + index).detach()
error: ->
alert 'Error'
但我想尝试学习更多的CoffeeScript语法的复杂的。我知道我可以使用括号作为$.post
,但允许链接回调,这与此$.ajax
格式不同。
$.post("/history", {food: food, size: size, unit: unit}, (data)->
alert 'Success'
).fail ->
alert 'Fail'
我尝试以下,但它从来没有所谓的成功回调:
$.ajax type:'DELETE', url: '/history', data: {id: id},
success: (data)->
alert 'Success'
$('#row'+index).detach()
error: ->
alert "Could not delete the food."
这个工作!
$.ajax type:'DELETE', url: '/history', data: {id: id}, success: ((data)->
$('#row'+tmpIndex).detach()
), error: ->
alert "Could not delete the food."
我不知道CoffeeScript的,但我敢打赌,你需要成功的功能和'误差之间的逗号:' – Barmar