[Django]-AJAX success not executing

3👍

You need return in anwer – JSON
For example

def answer(request):    
    # same you code
    payload = {'success': True}
    return HttpResponse(json.dumps(payload), content_type='application/json')

2👍

In the jQuery.ajax docs I can read this:

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

So, I would follow the example there and try if this works instead:

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.ajax( "example.php" )
    .done(function() { alert("success"); })
    .fail(function() { alert("error"); })
    .always(function() { alert("complete"); });
// perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() { alert("second complete"); });

Adapted to your code, it may look like this:

var jqxhr = $.ajax( "/eventsearch/eventsearch/createCustom/", {
     data: {name: name, loc: loc, start: start, end: end, tags: tags, event_id: event, profile: user}
    })
    .done(function() { alert("success"); })
    .fail(function() { alert("error"); })
    .always(function() { alert("complete"); });

I’m not completely sure about data, I was just following the docs.

Also, it should be good to add mimetype to the response like this:

return HttpResponse('', mimetype="application/json")

I think I’ve read somewhere that Ajax won’t work if this is not set with Django, I don’t remember where, I’m trying to find it on Google, give it a try.

Hope this helps!

Leave a comment