Simon Mathewson
Simon Mathewson

Reputation: 731

Can't filter data from AJAX request

I am trying to get new data from index.php via AJAX and insert it into an element. Unfortunately, jQuery won't filter the data correctly. I use the same options, that have been suggested here.

Corrected version:

$.ajax({
    type: "GET",
    url: "index.php",
    data: "ajaxGetContent=1",
    success: function(data){
        $(data).filter(".bubble").html();
        $("#bubbles").empty();
        $("#bubbles").append(data);
    }
});

When executing this code, this error message shows up in the console:

"Uncaught Error: Syntax error, unrecognized expression: All the requested HTML code"

Upvotes: 1

Views: 1507

Answers (1)

jeroen
jeroen

Reputation: 91762

You are not using the filtered data when you add it to your #bubbles element. Note that data remains unchanged in the first line of your success function.

You probably want something like:

success: function(data){
    var filtered_data = $(data).filter(".bubble").html();
    $("#bubbles").empty();
    $("#bubbles").append(filtered_data);
}

Note that the name of your url suggests that you are reloading your whole page with the ajax request and then using only a small part of it. You should make that small part a separate include in your original page and use that script to load using ajax so that you don't have to filter at all.

That would also reduce the amount of data that needs to be sent and the redundant processing that is now done on the server and discarded right away.

Upvotes: 1

Related Questions