Solved

My jquery .load returns HTML in Chrome, but all other browsers it returns "0"

Posted on 2013-01-14
6
260 Views
Last Modified: 2013-01-20
This is the javascript (relevant line) - I'm using wordpress, hence the call to the admin page with an action parameter...
$j('#city_statistics-div').load('wp-admin/admin-ajax.php', { action: 'city_statistics', t: '-1' });

Open in new window


this is the php
/*
 * City & Timezone Form
 */
function ttw_city_statistics() {
	$city_id = $_REQUEST[POST_CITY_URL];
	if( $city_id ) {
		echo ttw_city_statistics_html( $city_id );
	}
	die();
}


function ttw_city_statistics_html( $city ) {
	if( -1 == $city) {
		$select  = 'Latitude <input type="text" size="15" name="latitude" id="latitude" value="" />';
		$select .= 'Longitude <input type="text" size="15" name="longitude" id="longitude" value="" />';
		$select .= 'TimeZone ' . ttw_timezone_dropdown_html( '+00:00' );
		$select .= '<input type="checkbox" name="daylight" id="daylight" />Observes Daylight Savings';
	} elseif( 0 < $city ) {
		$result = ttw_get_city_sp( $city ) ;
		$select  = 'Latitude <input type="text" size="15" name="latitude" id="latitude" value="' . $result->Latitude . '" />';
		$select .= 'Longitude <input type="text" size="15" name="longitude" id="longitude" value="' . $result->Longitude . '" />';
		$select .= 'TimeZone ' . ttw_timezone_dropdown_html( $result->TimeZone );
		$select .= '<input type="checkbox" name="daylight" id="daylight" ' . (($result->daylight) ? 'checked=checked' : '') . ' />Observes Daylight Savings';
	}
		
	return $select;
}

Open in new window


In Chrome it works great - I get the input boxes I'm looking for.  In all other browsers, instead of the html, a "0" is printed in the div....

What silly thing have I forgotten????!
0
Comment
Question by:PaultheBroker
  • 4
  • 2
6 Comments
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 38778025
Not sure how your code works in any browser. You're loading a php file which contains 2 functions and there is no explicit call to either of them. If you just call that script normally, you will get no output, because the actual functions are never called.
0
 
LVL 6

Author Comment

by:PaultheBroker
ID: 38778482
Sure.  The function is being called.  As  i said ... its Wordpress.  I omitted this as being irrelevant -

add_action( 'wp_ajax_city_statistics' , 'ttw_city_statistics' );

Not to give you a Wordpress tutorial, but the call to the admin-ajax.php page with the action=city_statistics triggers the wordpress 'action' "wp_ajax_city_statistics" which then causes ttw_city_statistics() to be called.  That's just the way WordPress likes to roll - it ensures the WordPress framework is loaded when you perform the ajax call.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 38778753
Ah. OK, that makes sense. Not sure I can really help on this one - sorry!
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 6

Author Comment

by:PaultheBroker
ID: 38779647
Update - I thought that the ajax call was working but now that I test it on another computer, it seems nothing is working...so this might be a general ajax thing.
0
 
LVL 6

Accepted Solution

by:
PaultheBroker earned 0 total points
ID: 38781605
OK - thanks for your help.  I'm officially a dolt....the ajax call I was making required me to be logged in to work...which is why it worked in Chrome (because I had logged in there) but not anywhere else.  

add_action( 'wp_ajax_nopriv_city_statistics' , 'ttw_city_statistics' );

is the way to call ajax from WordPress if you aren't logged in :S
0
 
LVL 6

Author Closing Comment

by:PaultheBroker
ID: 38798132
I figured this out finally....after a lot of wasted time!  I hope it helps someone else - or I might just delete it out of shame!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now