Pay money and get answers for your WordPress questions (more info)

ajax function request not working in IE, if I'm not logged in.

  • SOLVED

I have this ajax request...


dealerResults = function () {
var county = $('#dealer-county').val(),
town = $('#dealer-town').val(),
wp_nonce = '<?php echo wp_create_nonce("dealer_search");?>';
if(county != 0 && town != 0){
$.ajax({
type: 'POST',
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: { varCounty : county, varTown : town, action: 'dealer_search', nonce: wp_nonce },
success: function(data){
$(".dealer-wrapper").remove();
$("#dealer-results").append(data);
}
});
}
}



and this is the function in the functions.php


function dealer_search_fn(){
check_ajax_referer( 'dealer_search', 'nonce' );

$ajaxCounty = $_POST['varCounty'];
$ajaxTown = $_POST['varTown'];

$dealerResults = new WP_Query(array(
'post_type' => 'dealer',
'meta_query' => array(
array(
'key' => 'dealer_country',
'value' => $ajaxCounty,
'compare' => '='
),
array(
'key' => 'dealer_location',
'value' => $ajaxTown,
'compare' => '='
)
)
));

if ($dealerResults->have_posts()) :
echo '<div class="dealer-wrapper">';

while ($dealerResults->have_posts()) : $dealerResults->the_post();

global $post;

echo 'stuff here';

endwhile;

echo '</div>';
unset($dealerResults);
endif;
die;
}
add_action('wp_ajax_dealer_search', 'dealer_search_fn');
add_action('wp_ajax_nopriv_dealer_search', 'dealer_search_fn');
function dealer_sort($a, $b){ if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; }


It works absolutely perfect in Safari, Chrome, Firefox (logged in and not logged in)

Apart from one exception on safari and 1 machine, but I tested like 5 other safaris on other macs and its fine.

However, in Internet Explorer I get 0 as the result. This was driving me mad for ages. I then found something on stackoverflow that said try it when logged in.

So I logged in on IE and it works!!!

So it only doesn't work on IE if the user is not logged in. Can help me make it work in IE when the user is not logged in..


Thanks

Answers (3)

2013-02-01

John Cotton answers:

Could be a caching thing...

Try modding this line as follows:

url: '<?php echo admin_url('admin-ajax.php'); ?>?r='+Math.random();

Have you looked at the headers? If not, use the dev tools on IE to see what the headers are saying, might give you a clue.

But your code all looks right to me.


Josh Cranwell comments:

Hi John,

Thanks for posting, this I did try this earlier hence why asked ktrusak how do this (just incase I did it wrong)

But when I did it, it did not force work. It still kept return -1

Anyway after learning it maybe cache, I cleared cache in the developer panel in IE and it worked.

So now I've left this on Math.random() - will this eliminate caching now forever?


I might leave this question open for a bit just to see other peoples responses, but your answer has been much help.


Thanks
Josh


John Cotton comments:

<blockquote>So now I've left this on Math.random() - will this eliminate caching now forever?</blockquote>
Yes since each URL will be different and thus not retrieved from the cache.

<blockquote>
Hi Martin,

Thanks for your answer - but adding wp_ onto nonce just returned -1 in every browser.
</blockquote>
It would do - what you had was correct. That function takes the name of the query field or defaults to (_ajax_nonce or _wpnonce).


Josh Cranwell comments:

Hi John.

Just had a first time visitor in the site ia moment ago in IE and he called up and said there nothing was being returned.

Very worrying if its still happening sporadically for first time (non-logged in) users.


But works for me on all browsers still and my versions of IE. 8, 8 compatibly and 9






John Cotton comments:

Can you send a link?

2013-02-01

Kyle answers:

IE is probably cacheing the ajax requests and tripping over itself. Try adding a random num var to the end of the posts so they are unique for each ajax request


Josh Cranwell comments:

Where would I add this?


Kyle comments:

This is the tutorial I have used that walks through using a little js to use with your requests

[[LINK href="http://yoavniran.wordpress.com/2010/04/27/ie-caching-ajax-results-how-to-fix/"]]http://yoavniran.wordpress.com/2010/04/27/ie-caching-ajax-results-how-to-fix/[[/LINK]]


Josh Cranwell comments:

Thanks, I will see what happens with johns solution below, see what happens over the next 24hours - I'm going to do more testing.


Kyle comments:

Sounds good, yes John's solution looks good. Keep us posted

2013-02-01

Martin Pham answers:

Your problem may be in check_ajax_referer function. ([[LINK href="http://queryposts.com/function/check_ajax_referer/"]]http://queryposts.com/function/check_ajax_referer/[[/LINK]])
Please try this:

function dealer_search_fn(){

check_ajax_referer( 'dealer_search', 'wp_nonce' );

$ajaxCounty = $_POST['varCounty'];

$ajaxTown = $_POST['varTown'];

$dealerResults = new WP_Query(array(

'post_type' => 'dealer',

'meta_query' => array(

array(

'key' => 'dealer_country',

'value' => $ajaxCounty,

'compare' => '='

),

array(

'key' => 'dealer_location',

'value' => $ajaxTown,

'compare' => '='

)

)

));

if ($dealerResults->have_posts()) :

echo '<div class="dealer-wrapper">';

while ($dealerResults->have_posts()) : $dealerResults->the_post();



global $post;



echo 'stuff here';


endwhile;



echo '</div>';

unset($dealerResults);

endif;

die;

}
add_action('wp_ajax_dealer_search', 'dealer_search_fn');

add_action('wp_ajax_nopriv_dealer_search', 'dealer_search_fn');

function dealer_sort($a, $b){ if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; }


Josh Cranwell comments:

Hi Martin,

Thanks for your answer - but adding wp_ onto nonce just returned -1 in every browser.


Thanks for your suggestion.