0

Despite many questions regarding this I cannot seem to find some code that works for my situation, every time I run this code I end up with an undefined variable instead of the desired returned JSON.

My AJAX code is:

$.ajax({
    data: {"serial":account},
    url: 'http://127.0.0.1/MobilePHP/findCustomerName.php',
    dataType: 'json',
    success: function(data){
        window.alert(data);   
    }
});

And my PHP code is:

    <?php
header("Access-Control-Allow-Origin: *");
header('Content-type: application/json');

include 'dbConfig.php';
include 'connectDB.php';

//$account = $_POST['serial'];

$account = 14;


$sth = mysql_query("SELECT customer_name AS Name FROM customer_details  WHERE auto_id = $account ");

$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $rows ['CustomerName'][] = $r;
}
echo json_encode($rows);


include 'closeDB.php';

?>

And my JSON from my console is:

{"CustomerName":[{"Name":"Tullaroan"}]}

I am really unsure of why I cannot access these variables as it seems to return the right JSON on the console.

2
  • You're just alerting data, can we see the code where you're actually using it? Commented Feb 21, 2013 at 15:03
  • are you sure the call is succeeding? use fiddler or the network tab of your browser development window. you'll get to see the sent and received requests, let me know Commented Feb 21, 2013 at 15:09

1 Answer 1

1

Inside the success function, data is an object with a single property: CustomerName. To access that, you use data.CustomerName. That property is itself an array, so you'll need to access elements inside it using their index.

In your example, you have a single object in the array, so to access that object you'd do data.CustomerName[0]. That object also has a single property: Name, so to get the actual name out you'd do: data.CustomerName[0].Name, which would return "Tullaroan".

It might be easier to visualise the breakdown of the data object like this:

data = {"CustomerName":[{"Name":"Tullaroan"}]}
data.CustomerName = [{"Name":"Tullaroan"}]
data.CustomerName[0] = {"Name":"Tullaroan"}
data.CustomerName[0].Name = "Tullaroan"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that answer, I really did not understand the format of the object properly, you explained it well and I think will prove valuable to me in the future.
To the down voter, as self thought coder migrating my first native Java Android application to web based and potentially Cordova application the nuances of script based languages handling of arrays and JSON can be a little confusing. This question while basic and seemingly unresearched has been very educational to me and hopefully others. Thanks

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.