Ajax可自定义错误回调函数
Ajax Customizable Error Callback Function
所以这个函数非常有效。我还有六个按钮,不希望我的代码有大量重复代码。对于自定义部分,我希望每个回调都是不同的,例如,如果用户不是管理员,则附加的文本"Please Log In"是不同的。我该如何为每个按钮自定义?非常感谢。
error: (xhr, ajaxOptions, thrownError) ->
console.dir arguments
console.log("*| Status ", xhr.status)
console.log("*| Error", thrownError)
console.log("*| Ajax", ajaxOptions)
if (not username? or not password?)
$('#data-text').empty()
$('#data-text').append ("""<h1>Please Log In</h1>""")
$('#input_username').fadeTo(100, 0.1).fadeTo(200, 1.0);
$('#input_password').fadeTo(100, 0.1).fadeTo(200, 1.0);
$('#header_user').css "background-color": "#d34242"
$('#header_password').css "background-color": "#d34242"
$('#data-text').css "background-color": "#d38642"
else
$('#data-text').empty()
$('#data-text').append ("""<h1>Failed Log In</h1>""")
$('#input_username').fadeTo(100, 0.1).fadeTo(200, 1.0);
$('#input_password').fadeTo(100, 0.1).fadeTo(200, 1.0);
$('#header_user').css "background-color": "#d34242"
$('#header_password').css "background-color": "#d34242"
$('#data-text').css "background-color": "#d38642"
请将代码保存在CoffeeScript 中
在CoffeeScript中,您可以在函数中定义函数。不要让任何人告诉你CoffeeScript或JavaScript是一种函数式语言。函数是一流的对象,对象可以封装其他对象。
error: (xhr, ajaxOptions, thrownError) ->
console.dir(arguments)
console.log("*| Status ", xhr.status)
console.log("*| Error", thrownError)
console.log("*| Ajax", ajaxOptions)
needsAGoodName = (msg) ->
$('#data-text').empty()
$('#data-text').append(msg)
$('#input_username').fadeTo(100, 0.1).fadeTo(200, 1.0)
$('#input_password').fadeTo(100, 0.1).fadeTo(200, 1.0)
$('#header_user').css("background-color": "#d34242")
$('#header_password').css("background-color": "#d34242")
$('#data-text').css("background-color": "#d38642")
if not username? or not password?
needsAGoodName("""<h1>Please Log In</h1>""")
else
needsAGoodName("""<h1>Failed Log In</h1>""")
我删除了if
周围的括号,并在函数调用周围添加了它们以进行样式设置。无论哪种方式都有效。
相关文章:
- JavaScript回调函数
- 如何在回调函数中执行流
- 回调函数中传递参数的困难(Google Map API Markers)
- 如何正确地将参数传递给RequireJS回调函数
- 回调函数在python代码中离线
- 从AJAX回调函数中分离数据
- 赋值后的回调函数
- 如何在javascript回调函数中返回多个变量
- 函数表达式,返回回调函数
- 等待回调函数执行
- JavaScript回调函数和Google Feed API
- 如何从ajax成功回调函数中读取javascript变量
- 正在保存JavaScript内部回调函数中的值
- 有没有什么方法可以停止Jquery中的animate(也可以停止完整的回调函数)
- 如何在javascript中添加带有回调函数的按钮点击事件作为window.conf
- 在Golang回调函数中启用CORS
- 在初始函数完成之前调用回调函数
- jQuery Mobile:如何在$.Mobile.changePage之前运行回调函数
- 即使使用回调函数也无法返回值
- 为什么prototypjs观察到回调函数有绑定