2

This question is not new, however I've spent really plenty of time, but coudn't fixed it.

<form id="test_data_form" action={% url 'insert_test_data' %} method="post">{% csrf_token %}
        <input type="submit" value="Insert test data">
        <div id="message"></div>
    </form>
    <script type="text/javascript">
        var form = $("test_data_form");
        $(document).ready(function() {
                    form.submit(function() {
                                $.ajax({
                                         data: form.serialize(),
                                         url: form.attr('action'),
                                         cache: false,
                                         type: form.attr('method'),
                                         dataType:'json',
                                         success: function(response) {
                                             alert("1");
                                         },
                                         error: function() {
                                             alert('Error!');
                                         }
                                        }
                                );
                                return false;
                            }
                    );
                }
        );
    </script>

views.py

@csrf_exempt
def insert_test_data(request):
    print request.is_ajax()
    data = {'success': True, 'html': "text"}
    return HttpResponse(json.dumps(data), mimetype="application/json")

urls.py

url(r'^hello/insert_test_data/$', insert_test_data, name="insert_test_data"),

Firstly, I've found that success isn't caught, so then while debugging I found that django doesn't think it is an ajax request and redirects me to the different page: /insert_test_data. And I want to stay on the home page.

I've found a lot of advices about that problem (e.g. cache: false) but none of them helped. I agree, that I miss something obvious, but couldn't figure out what exactly. I'll be realy glad for any help.

Dmitrii Bocharov
  • 872
  • 6
  • 21
  • Have you seen http://stackoverflow.com/questions/7755899/django-says-is-ajax-is-false-on-a-jquery-ajax-request? – alecxe Jun 23 '14 at 20:06
  • Yes, I have and I've tried adding `crossDomain: false`. It didn't help. – Dmitrii Bocharov Jun 23 '14 at 20:31
  • Check request headers by doing `print request.META` - Django detects AJAX requests by jQuery header by https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.is_ajax - most likely your custom $.ajax might not set this header and thus Django doesn't detect this being an AJAX request. – Mikko Ohtamaa Jun 23 '14 at 22:58
  • @MikkoOhtama yes. You're right. This header is not set. But why? As I understand I can set it explicily, but it's not good, I suppose, $.ajax must do it instead of me. – Dmitrii Bocharov Jun 24 '14 at 07:01
  • @BDSHADOW I guess that either 1) the header is present in the default $.get() requests 2) you have some kind of global jQuery AJAX configuration stripping out the header 3) jQuery behavior has changed in new versions – Mikko Ohtamaa Jun 24 '14 at 07:55
  • 1
    @MikkoOhtama I found an interesting thing. JS isn't executed at all. Thay is the reason why it is not ajax. It just use default form submit withou js. Now I'm trying to undestand why. It is strange – Dmitrii Bocharov Jun 24 '14 at 08:10

1 Answers1

0

The JavaScript to handle the form submit looks ok at first inspection, but there's one mistake:

var form = $("test_data_form");

If you use Firebug or Chrome developer tools and query for this element in the console, you won't get anything back because you're missing a selector (tag, id, class, etc). From your form tag, I think you meant to select by id:

var form = $("#test_data_form");
Brandon Taylor
  • 33,823
  • 15
  • 104
  • 144