How do i stop ?_=1447131652166 coming up on every Ajax Request

How do i stop ?_=1447131652166 coming up on every Ajax Request

SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2
edited November 2015 in Free community support

I'm trying to load JSON from a URL using DataTables, but it keeps adding ?_=1447131652166 (or similar) to the end of every request i make.

$(document).ready(function() {
    $('#example').DataTable( {
        "ajax":{
        "url":"http://localhost:8888/getPlayersGlobalStats", 
        "dataSrc": "players"
        },
        columns: [
        { data: "idPlayer" },
        { data: "barricadeWood" },
        { data: "barricadeWoodwire" },      
        { data: "bear" },
        { data: "bearTrap" },
        { data: "bleedings" },
        { data: "campfire" },
        { data: "cold" }
    ]
    } );
} );});

RESULT:

Failed to load resource: the server responded with a status of 400 (Bad Request) http://localhost:8888/getPlayersGlobalStats?_=1447131652166

Why is datatables passing something into my URL when i didn't request it?

The ?_=144713165216 on the end of the request is stopping my server from sending a response and causing it to fail.

How can i stop it?

[Removed broken second example, still in comments]

This question has accepted answers - jump to:

Answers

  • SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2
    edited November 2015
    $(document).ready(function() {
        $('#example').DataTable( {
            "ajax":{
            "url":"http://localhost:8888/getPlayersGlobalStats", 
            "dataSrc": "players"
            },
            columns: [
            { data: "idPlayer" },
            { data: "barricadeWood" },
            { data: "barricadeWoodwire" },      
            { data: "bear" },
            { data: "bearTrap" },
            { data: "bleedings" },
            { data: "campfire" },
            { data: "cold" }
        ]
        } );
    } );});
    

    RESULT:

    Failed to load resource: the server responded with a status of 400 (Bad Request) http://localhost:8888/getPlayersGlobalStats?_=1447131652166
    

    Why is datatables passing something into my URL when i didn't request it?

    The ?_=144713165216 on the end of the request is stopping my server from sending a response and causing it to fail.

    How can i stop it?

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    Answer ✓

    Thats done to prevent caching, if you look at the jQuery AJAX Documentation:

    cache (default: true, false for dataType 'script' and 'jsonp')
    Type: Boolean
    If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.

    DataTables sets this to 'false' to prevent caching, and for good reason. If you set it to true, you probably wont see any updates...

    But I think you can do something like..

    $('#data-table').DataTable({
        ajax: {
            url: 'whatever',
            cache: true
        }
    });
    

    That might work, because it looks like DT just sets defaults, then uses $.extend() to combine the ajax settings with the defaults. So give it a shot.

    If it works... then you shouldnt see any updates on ajax.reload()

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited November 2015 Answer ✓

    And your 2nd approach of using

    var dataArray= $.getJSON("http://localhost:8888/getPlayersGlobalStats", function(data){
         
    $(document).ready(function() {
        $('#example').DataTable( {
            "ajax": dataArray,
            "dataSrc" : "players",
            "columns": [
            { "data": "barricadeMetal" },
            { "data": "barricadeWood" },
            { "data": "barricadeWoodwire" },       
            { "data": "bear" },
            { "data": "bearTrap" },
            { "data": "bleedings" },
            { "data": "campfire" },
            { "data": "cold" }
        ]
        } );
    } );});
    

    Actually doesn't make any sense.. because you're actually passing the JSON result of the AJAX request as an ajax URL... Not only that, but dataArray is the xhr, you need to use the data

    This might work though

     $.getJSON("http://localhost:8888/getPlayersGlobalStats", function(data){
         
    $(document).ready(function() {
        $('#example').DataTable( {
            "data": data.players,
            "columns": [
            { "data": "barricadeMetal" },
            { "data": "barricadeWood" },
            { "data": "barricadeWoodwire" },       
            { "data": "bear" },
            { "data": "bearTrap" },
            { "data": "bleedings" },
            { "data": "campfire" },
            { "data": "cold" }
        ]
        } );
    } );});
    
  • SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2

    <3 jLinux, I knew i would be missing something simple somewhere, I set cache to true and it worked like a charm.

    I'm not using the API functions at this stage but thank you for the heads up.

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Ok cool, glad I could help!

    btw, dont forget to look at my 2nd post, might have missed the update I made

  • SamuelNZSamuelNZ Posts: 62Questions: 5Answers: 2
    edited November 2015

    Oh i did see that, And your response made me realize i had forgotten to stringify the response, But yeah that wouldn't have changed the fact i was passing it as a URL.

    Thank you for your help with the cache issue!!

    Edit: Going to paste an example of a previous usage so you can see where my chain of thought came from

    // make AJAX call   
            var dataArray= $.getJSON("URLHERE", function(data){
    
            // We succeeded so just do this
                 var Stats = [dataArray],
                    $ul;
                
              $(jQuery.parseJSON(JSON.stringify(dataArray))).each(function() {  
              console.log(this);
              $.each(data.players, function(index){
    
     //iterate through the returned data and build a list of Stats
                    Stats.push('<li id="' + this.playerName + '"><span class="ID"><Label>'+ this.playerName + '</label> | Steam 64:  ' + this.idPlayer + '</span>');
                    Stats.push('<span class="Kills"><label>Kills:  </label>' + this.kills + '</span><br />');
                    Stats.push('<span class="SKills"><label>Sleeper Kills:  </label>' + this.sleepers + '</span><br />');
                    Stats.push('<span class="DDeath"><label>Drowned:  </label>' + this.drowning + '</span></li><br />');
                    Stats.push('<span class="FDeath"><label>Falling Deaths:  </label>' + this.fall + '</span><br />');
                    Stats.push('<span class="CDeath"><label>Deaths From Cold:  </label>' + this.kills + '</span><br />');
                    Stats.push('<span class="LDeath"><label>Landmine Deaths:  </label>' + this.landmine + '</span><br />');
                    Stats.push('<span class="WDeath"><label>Wolf Deaths:  </label>' + this.wolf + '</span><br />');
                    Stats.push('<span class="BDeath"><label>Bear Deaths:  </label>' + this.bear + '</span><br />');
                    Stats.push('<br />');
                });
    
    

    ^ That isn't working, Just a copy pasta from the middle

This discussion has been closed.