#pass data from ajax to blade laravel

1 messages · Page 1 of 1 (latest)

grim forum
#

when we pass a list of data from database to blade, it it json ?
No. Blade is "rendered" on the server. So any data you pass is just in memory, it's used to create the resulting HTML

You're mixing two things here, blade is server only. It's rendered, returned to the browser and then that's it. Then you're making a request on the client, there is no blade on the client. You can modify the HTML in the browser through JS though

solar musk
#

@grim forum ah ic, so if i want to do this i have to create javascript variable and sent them to blade instead ?

grim forum
#

No, you can't send things to blade from the client

#

They're two different things

solar musk
#

ah sorry, i mean , use js to display them ?

grim forum
#

Exactly, in your success callback you have the data from the request, with that you can modify the HTML for example

solar musk
#

ah thank you

solar musk
grim forum
#

Not sure which echo you're referring to?

solar musk
#
        $comments = Product_comment::where('status','1')->where('product_id',$product_id)->where('reply_id',NULL)->get();

        echo json_encode($comments);
    }```
#

this one

solar musk
#

ah thank you again

solar musk
# grim forum <https://laravel.com/docs/10.x/responses#json-responses>

@grim forum do you know how to get an array of data ? i tried

        $comment_data = Product_comment::where('status','1')->where('product_id',$product_id)->where('reply_id',NULL)->get();

        $comment_json = array();

        foreach($comment_data as $row){
            $comment_json[] = array(
                'comment_id'   => $row["comment_id"],
                'username '    => $row["username"],
                'product_id '  => $row["product_id"],
                'comment '     => $row["comment"],
                'reply_id '    => $row["reply_id"],
                'star'         => $row["star"],
                'like'         => $row["like"],
                'dislike '     => $row["dislike"]
            );

        }
        return response()->json([
            'data' => $comment_json,
        ]);
    }```
but still get the 419 error, also can what should i put in the ajax ```data : {}``` ? and what in the success ?
grim forum
solar musk
#

i tried add

           headers: {
               'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
           }
       });```
under the ```$(document).ready(function(){```
but still get the error
grim forum
#

The meta tag would need to exist for that to work

solar musk
#

just add and still get that

#

and now the save data to db ajax code was work fine before getting the same error again

#

yay~~

solar musk
#

huh ?

#

i just took a nap

#

and now it run like nothing happen