0

In my jquery ajax request

$('#form').on('submit', function (e) {

            e.preventDefault();

            $.ajax({
                type: "POST",
                url: "/ajaxpage",
                data:{
                    selected_data: JSON.stringify(selected_data),
                    },
                dataType : 'json',
                success: function( data ) {

                    console.log(data);

                }
            });

        });

In my controller method im trying to access the array which i have sent from ajax by looping like this

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;

class AjaxController extends Controller
{

    public function postData(Request $request)
    {
        foreach ($request['selected_data'] as $data => $value)
        {
           $user->roles()->attach(role->id, ['pivot_colmumn' => $value]);
        }
    }
}

I think this is not a proper way. For single variable i use $request['var_name'] and it works but for array it gives 500 internal server error. So do i access the array values ?

0

6 Answers 6

3

You should decode your json that sent from ajax request in your controller!

$select_data = json_decode($request->get('select_data'));
foreach($select_data as $key => $value){}
Sign up to request clarification or add additional context in comments.

Comments

1

you can fix by add token for ajax setup:

In addition to checking for the CSRF token as a POST parameter, the VerifyCsrfToken middleware will also check for the X-CSRF-TOKEN request header. You could, for example, store the token in a HTML meta tag:

<meta name="csrf-token" content="{{ csrf_token() }}">

Then, once you have created the meta tag, you can instruct a library like jQuery to automatically add the token to all request headers. This provides simple, convenient CSRF protection for your AJAX based applications:

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

https://laravel.com/docs/5.5/csrf

1 Comment

I have done all that. only problem is that i dont know how to access array in laravel
0

I was actually using JSON.stringify(selected_data) in jquery ajax post request which actually converts it into a string.

So i need to use json_decode() to get an array then i can simply loop through it as usual.

Comments

0

For access to data that send from ajax request and convert it to array, you can do something like this in your controller :

foreach(json_decode($request->selected_data) as $data)
{
    dd($data)
}

Comments

0

You are trying to access the request object as an array when the request object is an object. Instead of using [ ] use ->.

Comments

0

did your mean $request->var_name and $request->all() ?

Laravel request returns collection

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.