1

I am trying to pass an array I am creating in PHP using json_encode(). I am running an AJAX request, see as follows:

AJAX CALL:

$.ajax({
        type: "POST",
        url: "../includes/ajax.php?imagemeta=1",
        data: {
            'id' : $(this).attr('id')
        },
        datatype:"json",
        success: function(data) {
            console.log(data);
            console.log(data["image_height"]);
        },
        error: function(data) {
            console.log(data);
        }
    });

PHP JSON RESPONSE:

if(isset($_GET["imagemeta"])){

$id = intval($_POST["id"]);
$path = "../uploads/images/" . $fm->selectImageById($id);
$meta = array();

list($width, $height) = getimagesize($path);
$meta["image_height"]   = $height . 'px';
$meta["image_width"]    = $width . 'px';

print json_encode($meta);

}

Although, the console.log(data) returns the following: {"image_height":"1374px","image_width":"920px"}

The following line console.log(data["image_height"]) returns undefined.

I have tried multiple attempts and have read through majority of the top rated questions regarding the subject. I am fairly new with JSON encoding so please inform me if I have a misunderstanding somewhere.

1
  • Try data.image_height? Commented Jun 28, 2014 at 1:03

3 Answers 3

4
datatype:"json",

should be:

dataType:"json",
    ^

Javascript is case-sensitive.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh my goodness. I hate my minor errors that throw me off for hours. Thanks a ton!
0

If console.log displaying you following:-

{"image_height":"1374px","image_width":"920px"}

Then you can access "image_height" as follows:-

data.image_height;

Comments

0

Either set php JSON response header or return plain text from php and use $.parseJSON in success function.

 success: function(data) {
        jSon = $.parseJSON(data);
        console.log(jSon.image_height);
    }

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.