1

$.ajax({ 
    url: '{{ route('add.orders') }}',
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    data: JSON.stringify({
     myArr : myArr
      }),
    cache: false,
    type: 'POST',
    dataType: "json",
    contentType: "application/json; charset=utf-8",
       success: function(data) {
       alert(data);
    }
    })

my controller:

public function addOrders(Request $request) {
        return $request->myArr;
    }

I'm getting this one:

this

this is myArr:

enter image description here

How can i reach the object? Why it seems [object Object] Can i reach myArr[0].id İf you help me i will be glad, thank you.

3
  • show your input field myArr . Commented Aug 8, 2021 at 14:36
  • I added now, thanks for your answer Commented Aug 8, 2021 at 14:46
  • Use console.log instead of alert function Commented Aug 8, 2021 at 14:51

2 Answers 2

1

You have to use like below

for(var i=0;i<data.length;i++){

    console.log(data[i].id);

    console.log(data[i].food_title);

}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes it's working, but i must use it in my controller, how can i reach it in my controller. If you help me i will be glad. Ex: $request->myArr[0]->id is not working
dd($request->all()) first and share your result
if I use dd() function i can not see any message on the console. so i have to use return $request->all() , i edit and added to my first message
Yes. You can't get any data in console log. Because it exit before return
1
$.ajax({ 
   url: "{{ route('add.orders') }}",
   headers: {
     'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
   },
   data:{ myData: JSON.stringify(myArr),_token:"{{ csrf_token() }}" },
   cache: false,
   type: 'POST',
   dataType: "json",
   contentType: "application/json; charset=utf-8",
   success: function(data) {
     console.log(data);
   }
});


#At Controller

public function addOrders(Request $request) {
    dd($request->all());
}

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.