0

My JSONP response from a remote domain is:

jQuery183012824459988766945_1354016515353([{"StudentID":"BA1122","LastName":"BAG","FirstName":"RON"},{"StudentID":"B770","LastName":"BAEN","FirstName":"AI"},{"StudentID":"B994","LastName":"BALD","FirstName":"AARON"},{"StudentID":"B580","LastName":"Balzstin","FirstName":"Manda"},{"StudentID":"B932","LastName":"BAR","FirstName":"ABBAS"},{"StudentID":"B139","LastName":"BANES","FirstName":"ALAN"},{"StudentID":"B718","LastName":"Baen","FirstName":"Alex"},{"StudentID":"B524","LastName":"BAER","FirstName":"ANA"}])

I'm trying to consume the data with:

$.getJSON('http://www.remote_domain.com/json.php?callback=?',function(res){
    ParseJson(res);
    function ParseJson(data){
        for (var key in data) {
            if (data.hasOwnProperty(key)) {
                alert(key + " -> " + data[key]);
            }
        }
    }
});

I seem to not get the data just the object?

UPDATE:

 $num_columns = $rs->Fields->Count ();
 $arrColumns = array();

 for ($i=0; $i < $num_columns; $i++) {
         $arrColumns[] = $rs->Fields($i);
         $newArr[] = $rs->Fields($i)->name; 
 }

 $arrResult = array();

 while (!$rs->EOF) {
     $arrRow = array();
     for ($i=0; $i < $num_columns; $i++) {
        $arrRow[$newArr[$i]] = $arrColumns[$i]->value;
     }
     $arrResult[] = $arrRow;

     $rs->MoveNext();
 }

 echo $_GET['callback'] . '(' . json_encode($arrResult) . ')';

My JSON gets returned like: [{"First":"John"},{"Last":"Doe"}] An array wrapped in an object. I need to return either an object or an array so I can handle in my client side like: {"First":"John"},{"Last":"Doe"}

<script>
    $.getJSON('http://remote.domain.com/json.php?callback=?',function(res){
        alert('Results: '+res.Last);
    });
</script>

I think it might be the way I'm json_encode($Array); and not using a class?

BarclayVision
  • 865
  • 2
  • 12
  • 41

1 Answers1

1

Try this

$.ajax({
    url: 'http://www.remote_domain.com/json.php',
    dataType: 'jsonp',
    success: function(data){

        $.each(data, function(k, v){
            console.log('key: ' + k + ' val: ' + v);
        });
    }
});
Johan
  • 35,120
  • 54
  • 178
  • 293
  • Just tried your example, no response back from server? isn't my initial `.$getJSON` doing the same thing and setting dataType as jsonp ? – BarclayVision Nov 27 '12 at 13:21
  • mine returns `key: 1 val: [object Object]` – BarclayVision Nov 27 '12 at 13:26
  • @JeffB Try `console.log(key, value);` if you want to inspect the object. – Johan Nov 27 '12 at 13:51
  • looks like the object is fine, just not sure I'm trying to parse it correctly : 0 Object { StudentID= "B081" , LastName= "BAG" , FirstName= "AON" } jsonp.html (line 17) 1 Object { StudentID= "B070" , LastName= "BASE" , FirstName= "A" } jsonp.html (line 17) 2 } – BarclayVision Nov 27 '12 at 14:10
  • @JeffB If you are trying to generate jsonp, you should wrap the entire json string in a function, not a function for each value. – Johan Nov 27 '12 at 14:21
  • Could you provide example? I'm struggling with this. - Could it have anything to do with my using ADO for my DB conn. The object would be ADO object. – BarclayVision Nov 27 '12 at 14:26
  • @JeffB Have a look here: http://stackoverflow.com/questions/1435072/how-to-generate-json-using-php – Johan Nov 27 '12 at 14:40
  • I guess I'm still not following? the console.log shows my json correct. I seem to be returning the json in format: `[{"xx":"yy"},{"aa":"bb"}]` First line of my post is my exact json returned... – BarclayVision Nov 27 '12 at 14:50
  • @JeffB Well in that case you just need to handle each value in whatever way you want. For example, `value[0]` is the first object. You can iterate over them using `$.each(value, function...` like above – Johan Nov 27 '12 at 14:55
  • I guess I'm back to my original question... HOW? – BarclayVision Nov 27 '12 at 15:04
  • still returning object not value - what am I missing? – BarclayVision Nov 27 '12 at 15:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20178/discussion-between-johan-and-jeff-b) – Johan Nov 27 '12 at 15:45