I'm retrieving data from a database via AJAX GET call, ater succesfull response I print the data by appending an html template to my table, I am getting the results back all right (in JSON format) but when appending them to table they all appear as undefined:
Here is my controller method:
public function index() { $reviews = Review::all(); return response()->json([ 'success' => 'Todas las opiniones recogidas', 'reviews' => $reviews, ]); }
This is my JQuery code where I append the results, I'm fairly certain the error is in here:
$.ajax({ async: true, url: '/reviews', type: 'GET', dataType: 'JSON', success: function (data) { $('.row[data-link=' + linked_entry + ']').remove(); $.each(data, function (index, item) { var reviews_row = '<tr class="row" data-link="reviews">'; reviews_row += '<td>' + data.body + '</td>'; reviews_row += '<td>' + data.author + '</td>'; reviews_row += '<td>' + data.site + '</td>'; reviews_row += '<td style="text-align:center;"><input type="checkbox" name="isVisible" '+(data.isVisible ? 'checked' : '')+'></td>'; reviews_row += '</tr>'; $('.entry_table_container[data-link=' + linked_entry + ']').append(reviews_row); }); }, error: function (data){ var errors = data.responseJSON; console.log(errors); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Change this:
$.each(data, function (index, item) {
To this:
$.each(data.reviews, function (index, item) {
and then:
item.body, item.author etc in your loop as one of the other contributors mentioned
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.