bioinformagician
bioinformagician

Reputation: 177

Javascript POST to Django via AJAX not working at all have run out of ideas

Hi we are just trying to post a javascript string variable to a django view. It sounds so easy but it is three days since I started trying to do this! I have practically read every relevant question on stack overflow and I just do not know what I am doing wrong..

JAVASCRIPT ON TEMPLATE HTML PAGE

<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>

<!--also have a jquery file imported further up in my code - 
<script type="text/javascript" 
src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
should be where it is getting ajax from, right?-->

<script language="JavaScript">
  $(function() {
  $( "#sortable" ).sortable();
  $( "#sortable" ).disableSelection();
  });

$(document).ready(function() {
$('#makeconstruct').submit(function()

{
        //Serializes the sortable's item id's into an array of string
        var senderStrIndexArray = $('#sortable').sortable();

    var linkOrder = $(senderStrIndexArray).sortable('toArray');
    alert(linkOrder);


    $.ajax({
        type: "POST",
        url: "/ecosystem/ajaxmes/",
        // The key needs to match your method's input parameter (case-sensitive).
        data: JSON.stringify({ linkOrder:linkOrder }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        //do we need json??
        success: function(data){alert(data);},
        failure: function(errMsg) {
            alert(errMsg);
        }
    });             
});
});
</script>

HTML ON TEMPLATE

<form id="makeconstruct" action="/ecosystem/ajaxmes/" method="post">
<!--DO WE NEED A METHOD/ACTION HERE IF IT IS DEFINED IN THE JAVASCRIPT??-->
{% csrf_token %}<!--view defined as csrf exempt but kept just in case-->
<ul id="sortable">
and a jQuery sortable list of things goes here in the code, each with an id
</ul>
<button type="submit">Submit</button></form>

VIEWS.PY

from django.utils import simplejson #not sure this is necessary given the code
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def ajaxmes(request):
    if request.is_ajax():
        message = "Yes, I am the king!!!"
    else:
        message = "No, I am going to cry!!"
    return HttpResponse(message)

URLS.PY

url(r'^ecosystem/ajaxmes', 'container.views.ajaxmes'),
#Think this is the only line I need to show here

The alert for the javascript linkOrder variable works, but then all we get is the "No, I am going to cry" response. We really are desperate now, thanks in advance for help.

Upvotes: 1

Views: 1929

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599580

You are not returning false from your Javascript submit function. That means that the Ajax is fired, but before it has a chance to do anything, the default action of the button happens and the form is submitted normally, which is why the is_ajax function returns false. If the JS returns false, the action will not be fired.

Upvotes: 2

Al W
Al W

Reputation: 7713

try changing this url(r'^ecosystem/ajaxmes', 'container.views.ajaxmes'), to this url(r'^ecosystem/ajaxmes/', 'container.views.ajaxmes'),

you are sending the request to "/ecosystem/ajaxmes/" but since in your urls.py you don't have the trailing slash, it is getting redirected.

Upvotes: 0

Related Questions