Home > Mobile >  how to parse javascript object to laravel component?
how to parse javascript object to laravel component?

Time:11-10

I am making an ajax get request to my laravel backend, the response (json response) includes array of products, in the ajax success function I am looping over the products array to render them in my html elements.

here is my controller response from laravel backend :

return response()->json(['products'=>$products]);

and here in my ajax success function (front end) I am looping over the products:

$.each(res.products, function(index, product){
    rest  = `{!! Theme::partial('product-item', compact('product')) !!}`;
});

I just need to put the product variable (loop parameters) in my compact function (loop body)

how can I parse the 'product' from the response to the 'product' in the compact function?

thanks in advance.

I made some researches about this problem but I was unfortunate

CodePudding user response:

What I suggest is this :

Try to console.log the variable products to get the structure of your json then this should do the trick

var products = await request.json();
index=0;
for(product in products)
{
    // depends on your json response
    console.log(product[index])
    // pay attention to the index you're trying to get
}
 

CodePudding user response:

Solved it, I was able to generate the html required before returning the ajax response, so I've generated the required data and returned it as a string, then in the blade component I had to concatinate the html string.

  • Related