Skip to content

Instantly share code, notes, and snippets.

@gaspardzul
Last active August 9, 2021 09:22
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save gaspardzul/1218844560a8405c6d40 to your computer and use it in GitHub Desktop.
Save gaspardzul/1218844560a8405c6d40 to your computer and use it in GitHub Desktop.
Este Gist te permite configurar las peticiones AJAX que haces en proyectos Django utilizando Jquery.
/**
* Este script de javascript permite trabajar transparentemente solicitudes que requieren
* protección del token CSRF por medio de AJAX JQUERY.
* Esto te permitirá hacer solcitudes a web Services de Django por medio de AJAX Jquery.
* Para utilizarlo basta con integrar el archivo DjangoAjax.js en tu directorio de JS y hacer referencia a él en tus templates
* que requieren del uso de AJAX por POST o algún otro que requiera el token CSRF.
* Este script está basado en la documentación oficial de Django https://docs.djangoproject.com
*/
$(function(){
//Obtenemos la información de csfrtoken que se almacena por cookies en el cliente
var csrftoken = getCookie('csrftoken');
//Agregamos en la configuración de la funcion $.ajax de Jquery lo siguiente:
$.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);
}
}
});
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));
}
// usando jQuery
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 csrfSafeMethod(method) {
// estos métodos no requieren CSRF
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
});
@daronwolff
Copy link

Pero si esto es una maravilla!! Muchas gracias!!

@vaj25
Copy link

vaj25 commented Jan 17, 2016

Me sera de mucha ayuda. Gracias.

@illkufe
Copy link

illkufe commented Mar 17, 2016

Muchas gracias Señor, será de muy util

@alexdzul
Copy link

alexdzul commented Jun 8, 2016

Super!!!! Me ayudará bastante :D

@ragnarok22
Copy link

Thanks

@Alfredynho
Copy link

definitivamente me salvaste la vida gracias Gaspar

@JCHR14
Copy link

JCHR14 commented Apr 17, 2018

Gracias bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment