Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial 
   
    Robert Walker
17,146 PointsCache false not working in a ajax request.
$("#button1").click(function(){
$.ajax({
            url: 'ajax/random.php',
            type: 'POST',
            data: {'method' : "10"},
            dataType: 'json',
            cache: false,
            success: function(data) {
                                         window.randomcard1 =  $("#randomCard1").card({
                            active: 0,
                            randomize : function(activeElementIndex){
                                return data.num1;
                                            }
    });
});
Every time I click the button it gives back the same random number over and over it never changes.
If I go directly to the page and echo out the results it works correctly I can see it changes results.
Ive tried it as a GET and also used the header control no-cache etc but nothing is working.
If I refresh the page and click again it works correctly gives me a new random number.
 
    Robert Walker
17,146 PointsThanks Adam but no that didn't so the trick either, it turned out that I had to use ajaxSetup cache false for it to work, so strange.
Thanks again though!
2 Answers
 
    Sean T. Unwin
28,690 PointsHave you tried logging to the console the returned data  to test to verify that you receiving something that you expect to get?
I think Adam Sommer has a good suggestion, although depending upon the type of element that $('RandomCard1') is you may need to update with text() ` instead of ' val() '.
I would also suggest that you think about changing window.randomcard1 to document.randomcard1 so that you don't unnecessarily pollute the global scope. window is considered the global scope when dealing with browser-based JavaScript. This suggestion will not affect your question directly in any way.
 
    Robert Walker
17,146 PointsI did and it was returning a new number correctly every time, even checking the header preview it was correct but for some reason it was getting cached by something ( assume JS as I mention above I had to use ajaxSetup cache false for it to finally work.
Thank you for replying too though very grateful.
 
    Sean T. Unwin
28,690 PointsI`m glad you got it working.
Although, it seems a bit odd yet interesting. The docs for jQuery.ajax() state that the cache property is not needed for POST requests, and that it works correctly with GET and HEAD requests which, to me, implies that it possibly may not work as intended with POST.
As an FYI, there is a warning for jQuery.ajaxSetup() which states it's use is not recommended.
Have you tried not using the cache property with your initial snippet above and see what happens?
 
    Robert Walker
17,146 PointsYeah the ajaxSetup was the only thing that worked for me, I have no idea why, I tried the GET and I tried PHP no cache too and only when I used ajaxSetup did it work.
Just before I used the same code without using any cache false with a POST and it worked fine so I honestly have no idea why it wasn't working on my own server (Rented of course).
Lucky for me it works fine on the guys server and doesn't need the ajaxSetup, it was just driving me mad trying to get it working.
Adam Sommer
62,470 PointsAdam Sommer
62,470 PointsI think inside your success function you need to update the value of the window.randomcard1 element using something like:
window.randomcard1.val(window.randomcard1.randomize())Or get the value of the randomize function into another variable then set the elements value to it with the val().
Hope that helps some.