skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search