Reputation: 735
Im trying to get my javascript to read the JSON from a url on another page of my site. The plan is to have an image with a link that's stored on that url and have that displayed on the front page of my site. I was able to do this before for a similar display so I thought it would be easy this time. For some reason, getJSON isn't even reading the JSON from my URL and I'm pretty sure it's correct, I modified the code from the previous display.
This code creates the JSON (in PHP):
function tf_feature_url_callback() {
// get last row from the table
$qry = 'SELECT * FROM tf_daily_featured_item ORDER BY fiid DESC LIMIT 1';
$result = db_query($qry);
$linkData = db_fetch_object($result);
$data = array('link'=>$linkData->link);
// encode the data as a json string
$json_link = json_encode($data);
echo $json_link;
}
And the JSON is displayed on a page as:
{"link":"test.com"}
This is the JavaScript:
// this is the url with JSON data
var urlURL = '/tf_feature/get_url';
$(document).ready(function() {
$.getJSON(urlURL, function(data) {
alert("TEST");
});
});
Im not getting an alert so i'm pretty sure my javascript isn't even entering the getJSON function.
Is there a limit to the amount of times you can call getJSON on a website? I'm calling it a few times already.
EDIT:
This is my callback (I'm using Drupal and this is a module)
// callback for the url
$items['tf_feature/get_url'] = array(
'title' => 'Featured Item URL Callback',
'page callback' => 'tf_feature_url_callback',
'access arguments' => 'access content',
'type' => MENU_CALLBACK
);
Upvotes: 1
Views: 147
Reputation: 35750
Ok, first off I have to say that I winced at urlURL
;-) But moving on, there's just not enough info here to fully diagnose your problem. Most likely your code isn't getting run; perhaps the page has already loaded by the time your onReady runs? Then again, maybe not; without more info it's hard to say.
I highly recommend either getting the Firebug plug-in for Firefox, or using the Chrome (built-in) developer tools. Either of these will be able to tell you whether the AJAX request is happening or not, and if it is happening what's coming back. Further more, you can add debugger
lines to your code, and both those tool will pause when the hit those lines, allowing you to see what's going on (you can inspect variable values and such).
Try using one of those tools, and I really expect you'll be able to debug this problem yourself.
Upvotes: 2