javascript - ajax request to the server side script (controller) to get the status of the server -
i want server side response button in twig page. example - there 2 button in page ---
on/off
when page load should check server (check elasticsearch database) whether returns true or false, both button.
so instanse on button return "true" should change color green, ----
$(this).addclass('btn-success'); $(this).removeclass('btn-primary');
the main purpose check what's current status, when page load.
so not on-click function, because show show status when page loaded.
this client side code ---
$(document).ready(function(){ //send request empty data server check status $.post('/url/to/action', {}, function(msg){ // return response {status: 'on'} client if search on if (msg.status == 'on'){ $('#my-button').addclass('success'); } // else disabled else $('#my-button').addclass('primary'); }); });
this integration symfony application ---
public function getstatusaction(request $request){ if ($output = !null) { return new \symfony\component\httpfoundation\jsonresponse(array('status' => 'on')); } else { return new \symfony\component\httpfoundation\jsonresponse(array('status' => 'off')); } }
this status when run status
now problem when page loading not rerurning status button. mean if the "status == 'on'" button should green instead of primary.
do knows how can fix problem.
to make that, can use setinterval
function refreshstatus () { $.post('/url/to/action', {}, function (msg) { // return response {status: 'on'} client if search on if (msg.status == 'on'){ $('#my-button').addclass('success'); } // else disabled else $('#my-button').addclass('primary'); }); } refreshstatus(); // call on page load setinterval(refreshstatus, 5000); // call refreshstatus every 5 seconds
Comments
Post a Comment