如何将数组传递给ajax并在控制器中访问?

时间:2017-09-22 05:44:23

标签: arrays ajax laravel

我有一个数组

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

如何将此数组传递给ajax并从控制器获取?

  $(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);
            }
        });
    });

1 个答案:

答案 0 :(得分:0)

ajax发布请求怎么样

  $(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);
            }
        });
    });

在您的路线中

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

然后您可以使用

在控制器上访问它
class SomeController extends Controller {
    public function specifiedControllerMethod(Request $request)
    {
        dd($request->all());
    }
}