有沒有一種方法可以將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