0

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 2

1

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.

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

Comments

0

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
}

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.