1

I'm trying to get a JSON encoded object from a PHP file via AJAX GET using jQuery. I'm not exactly sure what's wrong. Here's my request in Javascript:

function getInfo()
{
    $.ajax({
        url:'ajax/ipGet.php',
        type: 'GET',
        dataType:'json',
        success:function(response){
            console.log(response);
        }

    });
}

ajax/ipGet.php

<?php
include 'dbcon.php';


class ipInfo {
    private $ipAddress;
    private $status;
    private $serialNumber;
}

$ipInfo = new ipInfo;
$ipInfo->$ipAddress = "IP ADDRESS";
$ipInfo->$status = "ONLINE";
$ipInfo->$serialNumber = "TEST";

echo json_encode($ipInfo);

?>

I used GET because the user doesn't need to be redirected eventually. The PHP file will eventually retrieve values from a database.

André Dion
  • 21,269
  • 7
  • 56
  • 60
Carpetfizz
  • 8,707
  • 22
  • 85
  • 146
  • 1
    Look at what comes back from the server, use your debugger. – epascarello Oct 17 '13 at 00:49
  • 1
    First thing that stands out: if you're going to be accessing those class variables from outside the class like that, they need to be `public`, not `private`. Not sure if that's your problem, but it's certainly an issue. – JMTyler Oct 17 '13 at 00:51
  • 1
    take a look at this question: [Using json_encode on objects in PHP (regardless of scope)](http://stackoverflow.com/questions/4697656/using-json-encode-on-objects-in-php-regardless-of-scope) – shawnzhu Oct 17 '13 at 00:52
  • @epascarello, thanks, but I just get blank in my console. Also, I changed the private to public. – Carpetfizz Oct 18 '13 at 01:27

1 Answers1

1
  1. Change those private variables to public.
  2. Before you call echo at the end, call header('Content-Type: application/json');
  3. You might need to add accepts: 'application/json' to your $.ajax call.

Edit:

Also, remove the second set of $ in each line that you access the object variables. See below:

$ipInfo->$ipAddress = "IP ADDRESS";
$ipInfo->$status = "ONLINE";
$ipInfo->$serialNumber = "TEST";

should be

$ipInfo->ipAddress = "IP ADDRESS";
$ipInfo->status = "ONLINE";
$ipInfo->serialNumber = "TEST";
JMTyler
  • 1,604
  • 2
  • 21
  • 24
  • 1
    `dataType: "json"` forces `$.ajax()` to interpret the result as JSON, regardless of the `Content-type` header. – Barmar Oct 17 '13 at 01:06
  • Thanks, but I still don't receive anything in my console. The Network also shows a 200 OK on `ipGet.php`. – Carpetfizz Oct 18 '13 at 01:27
  • @Carpetfizz I've noticed another bug in your PHP code and edited my answer to describe it. Give that a shot and let me know how it goes! – JMTyler Oct 18 '13 at 16:42
  • +1 for the edit; that's an easy mistake to make, and I think is the actual issue. For an explanation @Carpetfizz, PHP is trying to retrieve the value of `$ipAddress` (which is `''`) and set a nonexistent attribute on the `$ipInfo` object. So `json_encode()` on an empty object is probably encoding nothing to nothing, assuming it isn't erroring out before that. – Izkata Oct 18 '13 at 16:45