1
var app = angular.module('myApp', ['ridge-speedometer']);
app.controller('customersCtrl', function($scope, $http, $timeout) {
    $scope.getData = function(){
    $http.get('asd.php').then(function (response) {
        $scope.myData = 33; ////////// response.data
    });
};

// Function to replicate setInterval using $timeout service.
$scope.intervalFunction = function(){
    $timeout(function() {
        $scope.getData();
        $scope.intervalFunction();
    }, 1000)
};

// Kick off the interval
$scope.intervalFunction();

Hi guys, as you see the codes above, I just have problem that line:

$scope.myData = 33; 
$scope.myData = response.data" 

when I'm writing 33 to the variable it works, but with "response.data". It isn't working. How can I get the value of my JSON.

This is my JSON page

[{"value":"23"}]

I have to get this 23

Thanks.

4 Answers 4

1

Try this code, You need to access the index[0] of the response

$scope.getData = function(){
    $http.get('asd.php').then(function (response) {
      $scope.myData = response.data[0].value;
});
Sign up to request clarification or add additional context in comments.

Comments

0

Like this.You have JSON response

[{"value":"23"}]

Get value

$scope.myData = response[0].value;

See Demo

var response= '[{"value":"23"}]';
object = JSON.parse(response); //parses your json into object
console.log(object[0].value);

Comments

0

Try get it with response.data[0].value.

Maybe you have to JSON.parse(response.data)[0].value to get it.

Comments

-1

In $http service calls , data response is usually in this format

enter image description here

I suggest you to get data like this

var response = response.data.data;

// now you can assign like you are doing
$scope.myData = response.value;

// or if your response is an array
$scope.myData = response[0].value;

I hope this helps. Thanks

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.