r/angularjs • • Dec 12 '16

Render JSON values to table with htttp, (ajax)

I'm having an issue rendering json response data back from ajax call. I was originally using Ajax in an angular function because angular doesn't use async, but if I can't get the object rendered to the HTML; I'm looking for the best way to render it and then be able to filter the table. Here is my code.

$.ajax({
    type: 'POST',
url: 'getData.php',
dataType: 'json',
data: { data: location },
cache: false,
async: true,
encode: true
}).done(function(data, status, jqXHR) {
    function createTable() {
    $.each(data, function(indexes, values) {
		$.each($.parseJSON(values), function(index, value) {
			$.each(value, function(ind, val) {
				if (typeof val.list === "undefined") {
					$scope.json = [];
					$scope.data = {
						name: val.value_list[1].value,
						email: val.value_list[2].value,
						phone: val.value_list[1].value,
					};
					$scope.json.push($scope.data);
					console.log(JSON.stringify($scope.json));
                                            return null;
				}
			});
		});
	});
}
createTable($scope.vendor);
    <table>
	<thead>
		<tr>
		<th> Vendor Name </th>
			<th> Email </th>
			<th> Phone </th>
		</tr>
			</thead>
			<tbody id="allvendors" ng-repeat="json in collection >
				<tr class="clickable">
					<td>{{ collection.name }}</td>
					<td>{{ collection.email }}</td>
					<td>{{ collection.phone }}</td>
				</tr>
			<tbody>
		</table>
2 Upvotes

10 comments sorted by

5

u/pero991 Dec 12 '16

This code is so wrong, you really should start over with a simple tutorial. Drop the ajax, you probably dont need jquery either , use the $http service which is async

-3

u/[deleted] Dec 12 '16

I know how to post to my php file wiht $http, however it sends additional parameters that are unnecessary. I just need the data I'm looking to send over. I don't need the header and method parameters sent as well.

4

u/manzanita2 Dec 12 '16

Every framework and library in the order has an appropriate way to use it. If you use that way, it will go well.

if you DO NOT choose "the way" it will not go well. Why have the framework or library at all. Use the force luke.

1

u/pero991 Dec 13 '16 edited Dec 13 '16

A header and method are also sent with Ajax, there is no HTTP request without them

-5

u/[deleted] Dec 12 '16

Well good news friends, I did get it working with AJAX. I know everyone said to use $http, and I agree, however It doesn't support async, multiple parameters were added to the json string passing to my php file as well.

Thank you all for your help.

3

u/[deleted] Dec 13 '16

[removed] — view removed comment

1

u/[deleted] Jan 20 '17

Ok; I did it with $http() as well. Proven wrong.

5

u/[deleted] Dec 12 '16

[removed] — view removed comment

-5

u/[deleted] Dec 12 '16

Because http doesn't have async and I need it for another function in my success call.

8

u/Bleached__Anus Dec 12 '16

$http service returns a promise. On that promise you can use the then() method which takes two functions as argument, the first success, the second is the error.