refresh var inside ajax data

refresh var inside ajax data

kezman10kezman10 Posts: 12Questions: 3Answers: 0
edited February 2023 in Free community support

Hello, I am using datatable together with ajax and jquery, in a php environment, I present the case

When loading the content I load a var of value o and the ajax call of dataable, at this point it is correct

this code

        var idReg=0;
    
    var tableReg = $('#ListRegistro').DataTable( 
        
           {    
    
               "ajax": {
               "url": UrlBase+"?type=4",
               "contentType": "application/json",
               "type": "GET",
           "data": {idServicio:idReg},
                "success": function ( d ) {
      return JSON.stringify( d );
          }
      }
     } );

Additionally, I have a function that changes the value of idReg, and it changes it correctly, but if I do an ajax.refresh, the data table request has not updated that variable...how can I update it or create that dynamic var?

thank you

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Two things - first, as discussed on the page ajax, success must not be overridden as it's used internally by DataTables.

    Secondly, you can use ajax.dataSrc to achieve what you want. This can be a function, so you can dynamically change where the data presides. Likewise, you can do the same with a function for ajax.data,

    Colin

  • kezman10kezman10 Posts: 12Questions: 3Answers: 0

    Could you show me an example please?

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Answer ✓

    For idReg use:

    data: function (d) {
      d.idServicio = idReg;
    }
    

    Then the function will be evaluated whenever you make an Ajax request.

    For the first part, can you show me a sample of the JSON data being sent back by the server?

    Allan

  • kezman10kezman10 Posts: 12Questions: 3Answers: 0

    Thanks its Works!!

  • kezman10kezman10 Posts: 12Questions: 3Answers: 0
    edited February 2023

    thanks

Sign In or Register to comment.