Change headers on ajax.reload() call
Change headers on ajax.reload() call
I have a datatable that uses an authentication header to get data from the API and it works fine, however, the token send in the header has an expiration time, I am capturing the api call error when the token expire but I can't find a way to update the headers options in ajax.reload(), is it possible to modify the headers on ajax.reload() call ?
My implementation is using Datatables with Vue, so I'm not sure how far I can go changing the ajax options object there.
This question has accepted answers - jump to:
Answers
I have something like this in the ajax object options, I am getting the token from a Vue store:
and have a watcher that on getting a new token it will just run
it runs the ajax call but loads the previous token and not the new one, resulting on an API error, so I would like to know if could modify the headers on the ajax.reload() call
The
ajax
docs state this:The jQuery.ajax docs state that the headers option can be a plain object. If you could pass a function then I would say it could be dynamic. I don't believe there is anything in Datatables to dynamically change the value.
Likely you will need to
destroy()
the Datatable and reinitialize to pick up the new token value instead of usingajax.reload()
.Kevin
Tried with beforeSend without luck, will check on your recommendation
Just if somebody wonders, yes, beforeSend was the correct approach to overwrite the headers, I was looking to the incorrect function to overwrite the headers, they should be something like: