1

I'm trying to get JSON data out of a $.get() jQuery call, but I can't seem to make it work properly.

Here's what my code looks like:

var url = "getDetailsJSON.php?ImageID=" + escape(itemName);

$.get(url, function (data) {

    console.log("Success!");
    var $detailDiv = $("#description");
    var itemDetails = $.parseJSON(data); // Is this how I would get the JSON object in the php code? 
    console.log("success" + data);
    var children = $detailDiv.children();
    for (var i = children.length; i > 0; i--) {
        $detailDiv.remove(children[i - 1]);
    }

    var descriptionP = $("<p></p>");
    descriptionP.text("Description: " + itemDetails.description);
    $detailDiv.append(descriptionP);

    var priceP = $("<p></p>");
    priceP.text("Price: $" + itemDetails.price);
    $detailDiv.append(priceP);
    var list = $("<ul></ul>");
    $.each(itemDetails.urls, function (index, value) {
        var url = itemDetails.urls[index];
        var li = $("<li></li>");
        var a = $("<a></a>");
        a.attr("href", url);
        a.text(url);
        li.append(a);
        list.append(li);
    });
    $detailDiv.append(list);
});

Here's the PHP code:

<?php

require_once('JSON.php');
$json = new Services_JSON();

$itemGuitar = array(
  'id' => 'itemGuitar',
  'description' => 'Pete Townshend once played this guitar while his own axe was in the shop having bits of drumkit removed from it.',
  'price' => 5695.99,
  'urls' => array('http://www.thewho.com/',
                  'http://en.wikipedia.org/wiki/Pete_Townshend')
);

$itemShades = array(
  'id' => 'itemShades',
  'description' => 'Yoko Ono\'s sunglasses. While perhaps not valued much by Beatles fans, this pair is rumored to have been licked by John Lennon.',
  'price' => 258.99,
  'urls' => array('http://www.beatles.com/',
                  'http://johnlennon.com/',
                  'http://www.yoko-ono.com/')
);

$itemCowbell = array(
  'id' => 'itemCowbell',
  'description' => 'Remember the famous "more cowbell" skit from Saturday Night Live? Well, this is the actual cowbell.',
  'price' => 299.99,
  'urls' => array('http://www.nbc.com/Saturday_Night_Live/',
                  'http://en.wikipedia.org/wiki/More_cowbell')
);

$itemHat = array(
  'id' => 'itemHat',
  'description' => 'Michael Jackson\'s hat as worn in the "Bille Jean" video. Not really rock memorabilia, but it smells better than Slash\'s tophat.',
  'price' => 1699.99,
  'urls' => array('http://www.michaeljackson.com/',
                  'http://music.yahoo.com/vid-2143030--Billie-Jean')
);


$details = array (
  'itemGuitar'  => $itemGuitar,
  'itemShades'  => $itemShades,
  'itemCowbell' => $itemCowbell,
  'itemHat'     => $itemHat
);

$itemDetail = $details[$_REQUEST['ImageID']];
$output = $json->encode($itemDetail);
print($output);

?>    

The 500 internal server error shows:

Connection  close
Content-Encoding    gzip
Content-Length  20
Content-Type    text/html
Date    Sun, 01 Sep 2013 22:47:32 GMT
Server  Apache/2.2.22 (Ubuntu)
Vary    Accept-Encoding
X-Powered-By    PHP/5.3.10-1ubuntu3.7

One of the problems with this code is that $.get() doesn't work as expected, because I keep getting a 500 internal server error. Once that is solved, I'm not sure how I'm suppose to extract that JSON data in a PHP file that contains JSON data (see the comment question in code). Any solutions for this?

3
  • 1
    The 500 server error is occurring on your server and has little to do with your .get. Fix that first. Then look at using .getJSON() instead. Commented Sep 1, 2013 at 21:18
  • To check the 500 error (without going about the error logs) just open up whatever variant of developer tools you might have (F12 on Chrome or get Firebug for Firefox) go the the "Network" tab, filter by XHR and click on the URL for your request. This should bring up the error passed by PHP. Commented Sep 1, 2013 at 21:55
  • I have updated the code, but I still can't figure out exactly what is causing the 500 internal server error. Commented Sep 2, 2013 at 1:02

2 Answers 2

1

as pointed out by @joshjwalker, you might use

$itemDetail = $details[$_GET['ImageID']];
echo json_encode($itemDetail);

and your js script might be

getJSON("getDetailsJSON.php", 
   {"ImageID" : escape(itemName)}, 
   function(data){
      console.log(JSON.stringify(data))
   }
);
Sign up to request clarification or add additional context in comments.

Comments

0

The first step is to find your apache error log. That will usually tell you what your 500 server error is, because there is some sort of error happening on your php side code.

Secondly, that is how you parse a json array from php, but are you using json_encode to encode your php data as json in your php file?

http://php.net/manual/en/function.json-encode.php

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.