M
M
Max2016-04-07 13:55:32
JavaScript
Max, 2016-04-07 13:55:32

Why does data transfer not an array but the entire page?

Hello, there is a piece of code like this:

$(".btn.btn-primary.place_block").click(
        function() {
            $arr = [];
            $link_id = $(location).attr('href').slice(36);
            $('.btn-warning').each(function() {
                $place_id = $(this).attr('id');
                $arr.push($place_id);
            });
            $.ajax({
                url: "/film_sessions/" + $link_id,
                type: "get",
                data: { data_value: $arr },
                // dataType: "html",
                complete: function() {},
                success: function(data) {
                    if ($('#data2').html() == '') {
                        $('#data2').append(data);
                    } else {
                        $('#data2').empty();
                        $('#data2').append(data);
                    }
                },
                error: function() {
                    alert("Ajax error!")
                }
            });
        });

Please tell me why the ajax request returns not just the $arr array, but the whole page.
That is, in the #data2 block, an exact copy of the page is displayed, and not just the array I need, which I pass in this line data: { data_value: $arr }.
console.log(data) - Displays the html code of the page in the console.
If I request an array directly -
success: function($arr) {
                    if ($('#data2').html() == '') {
                        $('#data2').append($arr);
                    } else {
                        $('#data2').empty();
                        $('#data2').append($arr);
                    }
                }

The code of the whole page is displayed in the same way.
Standard view of the page
b98a3e7eb718409e96642d88ea2dd2f7.png
View when several blocks are selected (at the moment the block is selected, the script is triggered. The id of the selected block is written to the $arr array)
13f6a03d4a684fd8a7a209767689e1bf.png
And this is what the script returns to the #data2 block looks like (The block is hidden by default, I use fancybox)
b224e48a0b19444192a790964c23c948.png

On the server, I receive this array and send it to a variable. This can be seen in the last screenshot (above).
Update
On the server side, this line takes the value data_value:
@test_test = params[:data_value] || []
Here is the code for the #data2 block
<div id="data2">
        <% if @test_test.present? %>
          <% @test_test.each do %>
            <h1>Hello</h1>
          <% end %>
        <% end %>
      </div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
polar-bear, 2016-04-07
@polar-bear

And if you set dataType: 'json' in the ajax configuration and return a response from the server with Content-Type: application/json?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question