开发者

AJAX Callback not being called

This is my ajax function:

function ajax_call(call_method,data_to_send) {
    logger("function ajax_call. var data_to_send: ");
    logger(data_to_send);
    $('.clickable save_button').hide()
    $.ajax({
      type: 'POST',
      url: call_method,
      data: data_to_send,
      success: function(data){
            logger("data returned to page after ajax call: ");
            logger(data);
            $('.error_msg').html("Successfully saved record to database.");
            $('.error_msg').fadeIn('slow');
            setTimeout("$('.error_msg').fadeOut('slow');",5000); // 5 secs to give user enough time to read
            $('.clickable save_button').show()
            response_dispatcher(data); // This should contain object type at least
      },
      failure: function(){
            $('.error_msg').html("There was an error while saving this information. Please try again. " +
                                    "If the error persists, please contact us using the contact form.");
            $('.error_msg').show;
            $('.clickable save_button').show()
      },
      dataType: 'json'
    });
}

And, this is the data sent to my method on the backend: { 'display_order':"3", 'goal':"dummy goal", 'id':-1, 'object_type':"goal" }

I've verified within my application that this same data is received.

Here is my Django view method:

@login_required
def update_goal_view(request):

    if request.method == 'POST' and request.is_ajax:
        # Example data sent from AJAX Request
        #qd = {u'display_order': [u'23'], u'object_type': [u'goal'], u'goal': [u'dummy goal'], u'id': [u'-1']}
        qd = request.POST
        goal = qd.get('goal','')
        display_order = qd.get('displ开发者_运维问答ay_order',99999)
        id = int(qd.get('id',''))
        object_type = qd.get('object_type','')

    # For now, just return something to test populating data to the page
    id = '100'
    goal = 'goal returned'
    object_type = object_type
    data = {'id':id,'goal':goal,'object_type':object_type}
    return HttpResponse(data,mimetype="application/json")

In Firebug, I see this after the ajax call:

POST http://127.0.0.1/xml/update_goal 200 OK 12ms

The issue is, when that it appears that my success callback is never called... I say that because as you can see from above, I there should be a message written to my logger but there isn't one. I know my logger works because of all the other messages outside of the callback that do get written to my logger.


I don't think Django does automatic serialization for dictionaries. You'll have to serialize them to JSON by hand.

import simplejson

# ...

return HttpResponse(simplejson.dumps(data), mimetype="application/json")


You don't show the code that triggers the ajax_call function. If it's as part of the onclick event for a submit button, or the onsubmit event for a form, the usual cause is that you've forgotten to prevent the form from submitting normally - so the page refreshes and the Ajax call is lost.

Use event.preventDefault() in your event handler to fix this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜