0

I have this ajax method in PostsController

public function ajax(Request $request)
    { 

        //dd($request);
        $this->authorize('view', Post::class);

        $posts = Post::orderBy("created_at","desc")->paginate(5);
        $comments = Comment::all();

        return response()->json(array("posts"=> $posts, "comments"=> $comments), 200);

    }

which works great when you just getting data and sending it. So i tried besides requesting data by ajax, to send some data alongside ajax request. How can i access that data inside controller?

Here is a method which resides inside certain blade:

function ajax(){

  let var1 = "gg";
  let var2 = "bruh";
  let token = document.querySelector("meta[name='csrf-token']").getAttribute("content");
  let url = '/posts';

  $.ajax({
    type: "POST",
    url: url,
    headers:
        {
            'X-CSRF-TOKEN': token
        },
    data: {
        'var1': var1,
        'var2': var2
    },
    success: function(data) {
        console.log(data);
    }
  });         

}

To simplify: How can i, dd() or dump(), given data(var1 & var2) by ajax function from blade in PostsController?

Here is route:

Route::post('/posts', "PostsController@ajax");

And here is some "gibberish" when i try to dd() it: gibberish

2 Answers 2

3

dd() is a laravel function and dump()for php. so you cannot use them from javaScript.

You cannot dd() or dump() from direct ajax request or JavaScript.

What you can do is, console log your data, or check from browser developer portion, network tab to see which data you are getting from the ajax response. You can find browser developer portion in,

for chrome:

Insepect > Network 

for mozila:

Insepect Element > Network 

If you are telling about get var1 and var2 on controller, you can just get them by $request->var1 and $request->var2.

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

9 Comments

Ok, let reform question: how to access that data so i can return it via response()->json() like i did with others? Edit: scratch this. I found out how. Edited controller method.
Open network tab, then, load the ajax. after that you will get the list of requests. then click on the request which you want to see response. Then you will get more 5 tabs. Goto to response tab. photo may helps you. imgur.com/a/LpZlVrv all process I saw from google chrome. it may vary from browser to browser.
I think you are telling about, how to catch data? If then, and if return response()->json(array("var1"=> $var1, "var2"=> $var2), 200); is your response, which you received by response variable, then, you can get var var1 = response.data.var1, and var var2 = response.data.var2
$request->var1 and $request->var2 Did you tried this?
To avoide gibberish , personally i use return $request; it suites me for debug the request data.
|
-1

Hasan05 was right. Just needed to know right direction. So to get data parameter of ajax request i modified ajax controller method:

public function ajax(Request $request)
    { 
        $var1 = $request->input('var1');
        $var2 = $request->input('var2');

        $this->authorize('view', Post::class);

        $posts = Post::orderBy("created_at","desc")->paginate(5);
        $comments = Comment::all();

        return response()->json(array("posts"=> $posts, "comments"=> $comments, "var1"=> $var1, "var2"=> $var2), 200);

    }

2 Comments

If @hasan05 was right, you should accept his answer. Not answer your own and not give him the proper credit for answering your question.
@Repox I did. Even if it didn't helped me by default, but one of his subcomments made me think in the right direction. There is no option accepting comments as accepted answers is it?

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.