有人可以解释为什么我的@model变量在我的ajax调用中不可用吗? 当我尝试在错误回调中使用@model变量时,它不存在。
$.ajax URL+ "/api/v1/menu_items/#{@model.id}/verify",
type: 'PUT'
data: formData
error: (response) ->
alert(response)
window.location.href = "/menu_items/#{@model.id}"
success: (data) ->
window.location.href = "/menu_items/#{data.id}"
答案 0 :(得分:0)
如果要在函数定义的外部范围内保留this
,请使用=>
fat arrow。
检查以下评论。
$.ajax URL+ "/api/v1/menu_items/#{@model.id}/verify",
type: 'PUT'
data: formData
# this reference to the `this' where $.ajax is called.
error: (response) => # a function definition here. use => to bind this to the outer scope
alert(response)
window.location.href = "/menu_items/#{@model.id}"
success: (data) ->
# in this function, this will be bound to the object on which the success callback is called, normally, null
window.location.href = "/menu_items/#{data.id}"