0

in my script, i have this line..

$scope.items = [{"id":"1","name":"name 1","description":"description 1","field3":"field3 1","field4":"field4 1","field5 ":"field5 1"}];

i made a data.php file to simulate the array value of $scope.items shown above

<?php
getaccessories = array("id"=>"1","name"=>"name 1","description"=>"description 1","field3"=>"field3 1","field4"=>"field4 1","field5 "=>"field5 1");

echo json_encode($getaccessories);
?>

the idea is to receive this array from data.php to $scope.items in my script via jquery.get(). i tried below statement with no result. how will it be done?

$scope.items = $.get("data.php", function(data){}, "json");

@sergiu, here's what you have asked me to do for data.php, please see if i did it right?

<?php

// $getaccessories = array("id"=>"1","name"=>"name 1","description"=>"description 1","field3"=>"field3 1","field4"=>"field4 1","field5 "=>"field5 1");

$getaccessories = array();
$accessory = new stdClass();
$accessory->id = "1";
$accessory->name = "name 1";
$accessory->description = "description 1";
$accessory->field3 = "field3 1";
$accessory->field4 = "field4 1";
$accessory->field5 = "field5 1";

$getaccessories[] = $accessory;


echo json_encode($getaccessories);
?>

------ EDIT for Sergiu ------

this is the output of chrome's console.log with the original value of $scope.items

[Object]
0: Object
        age: 50
        name: "Moroni"
  __proto__: Object
  length: 1
__proto__: Array[0]

and here's $scope.items with $.get()

Object {readyState: 1, setRequestHeader: function, getAllResponseHeaders: function, getResponseHeader: function, overrideMimeType: function…}
    $$v: Array[1]
        0: Object
            age: 50
            name: "Moroni"
        __proto__: Object
        length: 1
        __proto__: Array[0]

i'm hope this helps

SaintTron
  • 43
  • 1
  • 7

1 Answers1

2

You probably need

$.get("data.php", function(data){
    $scope.items = data;
}, "json");

Second argument to $.get is the callback, as in call it back for me when you receive the data I requested from the server.

I'm also guessing you have that $ in front of getaccessories.

You are expecting an array of objects. You can build it like this:

$getaccessories = array();
$accessory = new stdClass();
$accessory->id = "1";
$accessory->name = ="name 1";
...

$getaccessories[] = $accessory;
Sergiu Paraschiv
  • 9,929
  • 5
  • 36
  • 47