mardi 27 avril 2021

How to fetch the data from the server in jquery?

I use laravel and jquery.

I have 4 divs in my view. I need to fetch the time() from the server and to display it in each div's.

View:

<div id="div1"> </div>
<div id="div2"> </div>
<div id="div3"> </div>
<div id="div4"> </div>

Controller:

class SampleController extends Controller
{
    public function getcurrentTime($divId = null)
    {
     $result = time();
     return view('welcome');
    }
}

In Sample.js:

$(document).ready(function(){
  function myFunction() {
  document.querySelectorAll('*').forEach(function(node) {
    ajax = $.ajax({
      url: '/SampleController/getCurrentTime/' . node,
      method: 'post',
      dataType: 'json',
      success: function(data) {
         if (data === true) {
            trueFunction(data);
         } else {
            falseFunction(data);
         }
         ajax = null;
      }
    });

    node.innerHTML = ajax.data;

  });
}
  function trueFunction(data) {
      element.html('Success: ' + data);
   }

   function falseFunction(data) {
      element.html('Failed: ' + data);
   }

    });

In web.php

Route::post('SampleController/getcurrentTime', 'SampleController@getcurrentTime');

Getting an error

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.

Could someone please help.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire