Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

265
Views
How can I send JSON data from local storage to controller in Laravel?

I want to send the json data from view to controller using ajax. How can I achieve that?

The data in local storage is

[{"productId":2,"productCost":"630","productQty":"3"}]

Ajax

  function saveData(){
    const localStorageData=localStorage.getItem('myItems');
    $.ajax({
        headers: {
               'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
              },
                method:"POST",
                url: "{{route('user.plan.insertData')}}",
                dataType: "json",
                data:{localStorageData:localStorageData,
                },
                success:function(data){

                     alert(data);

                }
           });
}

Controller

    function insertData(Request $request)
{
    $localStorageData = $request->input('localStorageData');
    print_r($localStorageData);exit;
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

correct your ajax like this :

    data:{
        localStorageData: localStorageData,
        '_token' : '{{ csrf_token() }}'
    },

and also change the controller like this :

$localStorageData = $request->localStorageData;

remember to add @csrf inside (at the beggining) the form tag.

  • it depends on that what localStorageData really is. Depends on it, you can work with it. if it is a string , no problem but if it is an array, you should do different thing ...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!