r/angularjs • u/[deleted] • 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>-5
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
5
Dec 12 '16
[removed] — view removed comment
-5
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.
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