Crazy
Crazy

Reputation: 867

How to pass an array to ajax and access in controller?

I have an array

[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]

How can i pass this array to ajax and get it from controller?

  $(document).on('click', '#print', function(){
        var code = $(this).attr('data-code');
        $.ajax({
            type: 'GET',
            url: 'print',
            data: {code: code,option: array here},
            dataType: 'html',
            success: function (html) {
                w = window.open(window.location.href,"_blank");
                w.document.open();
                w.document.write(html);
                w.document.close();
            },
            error: function (data) {
                console.log('Error:', data);
            }
        });
    });

Upvotes: 0

Views: 2218

Answers (1)

Jerico Pulvera
Jerico Pulvera

Reputation: 1042

How about an ajax post request

  $(document).on('click', '#print', function() {
        var code = $(this).attr('data-code');
        var a =[1,2,3,4,5]

        $.ajax({
            type: 'POST',
            url: '/print',
            data: {code: code,option: a,"_token": "{{ csrf_token() }}",},
            dataType: 'json',
            success: function (html) {
                w = window.open(window.location.href,"_blank");
                w.document.open();
                w.document.write(html);
                w.document.close();
            },
            error: function (data) {
                console.log('Error:', data);
            }
        });
    });

In your routes

Route::post('/print', 'SomeController@specifiedControllerMethod);

Then you can access it on your controller using

class SomeController extends Controller {
    public function specifiedControllerMethod(Request $request)
    {
        dd($request->all());
    }
}

Upvotes: 1

Related Questions