Handling of $http’s replay.get

Asked

Viewed 600 times

1

Hello I’m looking for information in a local database with the following code:

var app = angular.module('app',[]);

app.controller('conexao',function($scope, $http){
    $scope.names = [];
    $http.get('http://localhost/angular/conect/connect.php').then(function successCalback(response){
            $scope.names = response.data;
            console.log($scope.names);
        },
        function errorCallback(response){
            console.error('Erro ' + response);
        });
});

To be displayed here:

<body ng-controller="conexao">
    <table>
        <tr ng-repeat="x in names">
            <td>{{x.nome}}</td>
            <td>{{x.idade}}</td>
            <td>{{x.cor}}</td>
        </tr>
    </table>
</body>

But all I get is an empty screen, although the console returns me:

>Object {records: Array[2]}

And then that message:

Synchronous Xmlhttprequest on the main thread is deprecated because of its detrimental effects to the end user’s Experience.

I want to understand what’s going on and know how to fix it.

Print dos Objects:

1 answer

2


The answer comes within a Object called 'Records'. Try to do so:

$scope.names = response.data.records;

Or else directly into your ng-repeat thus:

ng-repeat="x in names.records"

As for your other question, see if this topic resolves.

If not, try using yours $http thus:

$http.get('angular/conect/connect.php').then(
    function (response){
        $scope.names = response.data;
        console.log($scope.names);
    },
    function (responseErro){
        console.error('Erro ' + responseErro);
    }
);
  • Using this form it returns: [Object, Object]. As for the other topic I’m trying to understand the solutions, they seem to me vague. there are commenting from Jquery and I’m not using anything from Jquery here.

  • Okay, focusing on the first problem. Still you couldn’t display? I put one more option in my answer, take a look.

  • There’s still no answer. 'Response.data' returns: 'Object {Records: Array[2]}' 'Response.data.Records' returns: '[Object, Object]' Even when searching to add . Records in repeat does not change anything

  • Does the data inside the 'Objects' match correctly to the ones you are trying to display? Get a console print with the 'Objects' open?

  • It’s the data I really want, it’s the exact data I’m looking for from the BD. I’m going to put the image up there next to the question.

  • 1

    Note that your Object has fields with the first uppercase letter. Change this in your html as well. x.Name, x.Age and x.Color =D

  • It worked! o.0 kkkkkkk Can you explain to me why this happens? I have another example that make a get of an API and it works normally when I started using one . php gave this, I don’t know if it has to do, but all the settings I’ve made are in minuscules.

  • 1

    There it would be because it is a case sensitive process, that is, if it is uppercase or lowercase, it makes a difference. Now about your other problem, I will provide a $http.get template that I use. I believe your problem is happening because you’re going through url complete in the process.

  • Nothing has changed with this change in $http.get this problem still persists. Everything I’m finding concerning this involves Jquery, but this is not my case, I’m not using Jquery.

  • 1

    All I’m finding is about jQuery too. Now I’m running out of time, but I’ll see if I can get an answer for you on that.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.