here이 제공된 Django jQuery 수정은 iPad에서 작동하지 않습니다. 어떻게 작동하게하는지에 대한 아이디어가 있습니까?Django jQuery CSRF 수정이 iPad에서 작동하지 않습니다.
수정 코드 :
$(document).ajaxSend(function(event, xhr, settings) {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
function sameOrigin(url) {
// url could be relative or scheme relative or absolute
var host = document.location.host; // host + port
var protocol = document.location.protocol;
var sr_origin = '//' + host;
var origin = protocol + sr_origin;
// Allow absolute or scheme relative URLs to same origin
return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
// or any other URL that isn't scheme relative or absolute i.e relative.
!(/^(\/\/|http:|https:).*/.test(url));
}
function safeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
});
EDIT가
명확히하기 : 여기
은 샘플 JS이다 일반 브라우저에서function foo() {
data = {some:"datahere"}
jQuery.ajax({
url: "urlhere",
type: "POST",
data: data,
success: function() {
// do something
},
error: function() {
alert("Could not send ajax request with POST");
}
})
}
에러 함수이다 결코 전화를받지 못했지만, iPad에서는 항상 경고를받습니다. t (적어도 iOS 시뮬레이션에서). django 로그를 볼 때 그 요청은 코드 403과 함께 반환 된 것을 볼 수 있습니다. @csrf_exempt
데코레이터를 추가하면 모든 것이 iPad에서 작동하므로 csrf가 실패한 것입니다.
정확히 무엇을 의미하는 "아이 패드에서 작동하지 않습니다"는 무엇입니까? – Hoff
죄송합니다. 방금 질문을 업데이트했습니다. thanx @Hoff – miki725