how to send array from script to controler

Published 2 months ago by hecate0211

i want to send my array to controller, but i idont understand how to send it

this is my view

  <button class="btn btn-primary" id='save'>Import File</button>

and this is my script

document.querySelector('#save').addEventListener('click', function() {
                var dataTables = [];
                var rows = hot.countRows()-20;
                var cells = hot.countCols();
                for(i =1; i<rows;i++){
                      dataTables = [dataTables, [hot.getDataAtCell(i,0), hot.getDataAtCell(i,1),
                      hot.getDataAtCell(i,2),hot.getDataAtCell(i,3),hot.getDataAtCell(i,4),
                      hot.getDataAtCell(i,5)]];
                }
              });

i want to send my array dataTables to my controler can someone help me?

Best Answer (As Selected By hecate0211)
Dry7

@hecate0211

  1. in view
<script type="text/javascript">
                        function sendData() {
                            var data = [
                                1,
                                2,
                                3
                            ];
                            $.ajax({
                                url:'/test',
                                type: 'POST',
                                dataType:'json',
                                contentType: 'json',
                                data: JSON.stringify(data),
                                contentType: 'application/json; charset=utf-8',
                            });
                        }
</script>
  1. in controller
function saveJson(Illuminate\Http\Request $request)
{
    $data = json_decode($request->getContent());
}
  1. in app\Http\Middleware\VerifyCsfrToken
protected $except = [
        '/url-of-saveJson'
    ];
Dry7
Dry7
2 months ago (134,980 XP)

@hecate0211 use JSON.stringify in javascript and json_decode in PHP

hecate0211

@Dry7 but how to send the json to controller?? i try use

var jsonPost = JSON.stringify(dataTables);
                alert(jsonPost);
                
                $.ajax({
                    url:'/tes',
                    type: 'POST',
                    dataType:'json',
                    contentType: 'json',
                    data: {data:jsonPost}
                });

but i dont know if it work or not i still confuse can u teach me bro?

deansatch

Try it and find out. I presume you are wrapping that code in an 'on click' so that it actually fires when you click your save button?

Open your page in chrome.

Inspect the page

Click the 'network' tab

Click your save button and view the results. You should see 'tes' in the name column. Click on it to see a preview of the results

Dry7
Dry7
2 months ago (134,980 XP)

@hecate0211

  1. in view
<script type="text/javascript">
                        function sendData() {
                            var data = [
                                1,
                                2,
                                3
                            ];
                            $.ajax({
                                url:'/test',
                                type: 'POST',
                                dataType:'json',
                                contentType: 'json',
                                data: JSON.stringify(data),
                                contentType: 'application/json; charset=utf-8',
                            });
                        }
</script>
  1. in controller
function saveJson(Illuminate\Http\Request $request)
{
    $data = json_decode($request->getContent());
}
  1. in app\Http\Middleware\VerifyCsfrToken
protected $except = [
        '/url-of-saveJson'
    ];
hecate0211

@Dry7

(1/1) FatalThrowableError
Type error: Too few arguments to function App\Http\Controllers\RabController::saveJson(), 0 passed and exactly 1 expected

i got this error

hecate0211

@Dry7 this is my full code

view

<button class="btn btn-primary" id='save' onclick="sendData()">Import File</button>

<script type="text/javascript">
              function sendData() {
                  var data = [
                      1,
                      2,
                      3
                    ];
                  $.ajax({
                      url:'/test',
                      type: 'POST',
                      dataType:'json',
                      contentType: 'json',
                      data: {data:JSON.stringify(data)},
                      contentType: 'application/json; charset=utf-8',
                  });
              }
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

route

Route::post('/test','[email protected]');

controller

public function saveJson($request)
        {
            $data = json_decode($request->getContent());

        }
Dry7
Dry7
2 months ago (134,980 XP)

@hecate0211

public function saveJson(Illuminate\Http\Request $request)
        {
            $data = json_decode($request->getContent());

        }

or

use Illuminate\Http\Request; (in header)

public function saveJson($request)
        {
            $data = json_decode($request->getContent());
        }
hecate0211

@Dry7 i already use in my code

use Illuminate\Http\Request; (in header)

public function saveJson($request)
        {
            $data = json_decode($request->getContent());
        }

and got error


(1/1) FatalThrowableError
Type error: Too few arguments to function App\Http\Controllers\RabController::saveJson(), 0 passed and exactly 1 expected```

hecate0211

@Dry7 oh iknow my error thanks for ur big help bro

hecate0211

@Dry7 bro, when i send the array.. why i got null when use

 $data = json_decode($request->getContent());

and when i use

$data2 = $request->getContent();

the result i got string(20) "data=%5B1%2C2%2C3%5D"

Dry7
Dry7
2 months ago (134,980 XP)

@hecate0211 use

data: JSON.stringify(data),

in $.ajax

Please sign in or create an account to participate in this conversation.