logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

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

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

This question has been answered.

Josh Cranwell | 02/01/13 at 1:44pm Edit
Tutorial: How to assign prize money

Previous versions of this question: 02/01/13 at 1:46pm | 02/01/13 at 1:49pm

(12) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    02/01/13
    1:50pm
    Kyle says:

    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

    Previous versions of this answer: 02/01/13 at 1:50pm

  • avatar
    Last edited:
    02/01/13
    5:12pm
    John Cotton says:

    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.

    Previous versions of this answer: 02/01/13 at 5:12pm

  • avatar
    Last edited:
    02/01/13
    2:14pm
    Martin Pham says:

    Your problem may be in check_ajax_referer function. (http://queryposts.com/function/check_ajax_referer/)
    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; }

  • avatar
    Last edited:
    02/01/13
    2:00pm
    Josh Cranwell says:

    Where would I add this?

  • avatar
    Last edited:
    02/01/13
    2:02pm
    Kyle says:

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

    http://yoavniran.wordpress.com/2010/04/27/ie-caching-ajax-results-how-to-fix/

  • avatar
    Last edited:
    02/01/13
    2:54pm
    Josh Cranwell says:

    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

  • avatar
    Last edited:
    02/01/13
    2:55pm
    Josh Cranwell says:

    Hi Martin,

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


    Thanks for your suggestion.

  • avatar
    Last edited:
    02/01/13
    2:56pm
    Josh Cranwell says:

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

  • avatar
    Last edited:
    02/01/13
    2:59pm
    Kyle says:

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

  • avatar
    Last edited:
    02/01/13
    3:08pm
    John Cotton says:

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

    Yes since each URL will be different and thus not retrieved from the cache.


    Hi Martin,

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

    It would do - what you had was correct. That function takes the name of the query field or defaults to (_ajax_nonce or _wpnonce).

  • avatar
    Last edited:
    02/01/13
    5:22pm
    Josh Cranwell says:

    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





  • avatar
    Last edited:
    02/01/13
    5:22pm
    John Cotton says:

    Can you send a link?

This question has expired.



Josh Cranwell, Arnav Joy voted on this question.



Current status of this question: Completed



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.