我有以下 AJAX 调用:
$.ajaxSetup({
csrfSafeMethod: function(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
},
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
我得到:
csrfSafeMethod is not defined
为什么从 beforeSend
内部看不到 csrfSafeMethod
?
我该如何解决这个问题?
最佳答案
你不能像这样定义一个常规函数吗:
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
关于javascript - 在 Ajax 调用中调用辅助方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36633562/