jquery + ajax problems in elgg 1.8

I noticed a problem about jquery + ajax in elgg 1.8.

I'm trying to load using ajax a captcha.

$(document).ready(function(){
        $.ajax({
            url: "http://urlpetition",
            dataType: 'json',
            success: function(data){
                $('#mathcaptcha').html(data.output.q);
            }
        });   
});

I have two installs of elgg 1.8.1. The first install is a clean 1.8.1 install. The second one is an upgrade from 1.7.X to 1.8.1.

In the first install, I receive with $.ajax this response:

{"output":{"success":true,"q":"14 menos 12"},"status":0,"system_messages":{"error":[],"success":[]}}

In the second install, I receive this:

{"success":true,"q":"5 m\u00e1s 7"}

 

Where is the problem? I tried to delete views cache, tested jquery versions...
I have other plugins that use jquery and fail too because this.

  • Try using elgg.ajax instead of $.ajax

    in http://urlpetition, do

    json_encode($data);

    forward(REFERER);

    Set what that does

  • Thank you for your response. I investigated this issue a few more following your advises.

    The function that generates the JSON response is a page handler, so the end of this function is:

    echo json_encode($json);

    If I put after this echo forward(REFERER), I receive in the second install my json encoded object plus a rendered page with an error message (forward after headers sent). In my first install I receive again this type of response:

    {"output":{"success":true,"q":"14 menos 12"},"status":0,"system_messages":{"error":[],"success":[]}}

    But if I put after this echo die(), I just only receive my json encoded object.

    I know that this should be implemented in an action instead of a page handler, but I think that could be a bug of upgrading from 1.7.X to 1.8.1.

  • Make sure your page handler has return true;