Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6844505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:23:12+00:00 2026-05-27T00:23:12+00:00

The Django jQuery fix which is given here does not work on iPad. Any

  • 0

The Django jQuery fix which is given here does not work on iPad. Any ideas on how to make it work?

fix code:

$(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

To clarify:

Here is a sample 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");
        }
    })
}

In a regular browser, the error function is never called, however on iPad I always get an alert (at least in iOS sim). When I see the django log, I see that those requests were returned with code 403. And if add @csrf_exempt decorator, everything works on iPad, so I am pretty sure it is csrf failing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:23:12+00:00Added an answer on May 27, 2026 at 12:23 am

    iPad for some reason does not always get the value of the CSRF cookie, therefore jQuery csrf fix did not work since it was unable to read the value of the cookie.

    The solution is make sure the cookie is send to the client with a ensure_csrf_cookie decorator on the views which interact with the client which require csrf.

    example:

    # models.py
    from django.views.decorators.csrf import csrf_protect, csrf_exempt, requires_csrf_token, ensure_csrf_cookie
    
    @ensure_csrf_cookie
    def fooview(request):
        # do something here
        pass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how Django + Jquery and Ajax calls work together.
I am trying to make a livesearch with Django and jQuery. What I've done
I'm new to Django. Today I created some Django custom tags which is not
Does anybody know how to implement the jQuery autocomplete plugin in Django, using databases
Technologies being used Google app engine Django Python Jquery Details of the code and
I have a Django form which I load dynamically to a jQuery dialog after
I'm using django-ajax-selects , which is a freely available django app providing jquery autocomplete
am new to jquery.I am doing some ajax using jquery in django. $(document).ready(function() {
I've got a Django app that accepts uploads from jQuery uploadify , a jQ
I have a problem with the jquery-autocomplete pluging and my django script. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.