Я получил ошибку "Uncaught ReferenceError: jQuery не определен
at getCookie "для функции getcookie, определенной в js. Я использовал эту функцию для получения токена csrf для перехода к представлениям django.
Вот мой код объявления html-скрипта: -
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<link rel="icon" href="../../static/images/logo.png" type="image/gif" sizes="16x16">
<!-- CSS -->
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<link rel="stylesheet" href="../static/stylesheets/jquery-ui.css">
<link rel="stylesheet" media="screen" href="../static/stylesheets/spectrum.css" />
<link rel="stylesheet" href="../../static/stylesheets/bootstrap-select.min.css">
<link rel="stylesheet" media="screen" href="../../static/stylesheets/reconvert.mini.css" />
<link rel="stylesheet" href="../../static/stylesheets/main.css">
<!-- Js -->
<script type="text/javascript" src="../../static/admin/js/jquery-2.1.1.min.js"></script>
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> -->
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src="../../static/admin/js/bootstrap.js"></script>
<script src="../../static/admin/js/spectrum.js"></script>
<script src="../../static/admin/js/bootstrap-select.min.js"></script>
<script src="../../static/admin/js/nprogress.js"></script>
<script src="https://unpkg.com/tippy.js@4"></script>
</head>
<body>
<form action="/client/" method="post" id="post-form" class="container tab-pane">
{% csrf_token %}
<div class="form-group">
<label for="phone_number">Whatsapp Phone Number</label>
<input type="text" class="form-control" name="phone_number" id="phone_number" />
<br>
<label for="chat_butn_text">Chat Button Text</label>
<input type="text" class="form-control" name="chat_butn_text" id="chat_butn_text"/><br>
</div>
<br>
<button type="submit" class="btn btn-primary btn-block">Submit</button>
</form>
</body>
Вот мой код JS: -
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;
}
var csrftoken = getCookie('csrftoken');
/*
The functions below will create a header with csrftoken
*/
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
function sameOrigin(url) {
// test that a given url is a same-origin 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));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {
// Send the token to same-origin, relative URLs only.
// Send the token only if the method warrants CSRF protection
// Using the CSRFToken value acquired earlier
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
Я все еще обнаружил, что jQuery не определил ошибку, хотя я и правильно объявил в своем теге скрипта. Любая помощь будет высоко ценится!!
Спасибо!