E
E
Emil Rakhmatullin2019-09-03 12:18:55
JSON
Emil Rakhmatullin, 2019-09-03 12:18:55

How to make an AJAX request on one page and get the result on another?

I use AJAX (JSON data type) and Laravel.

Wrote an AJAX request that gets a new record of the current feed:

var myJsonData = {
  "_token": "{{ csrf_token() }}",
  id: '{{ $feed->id }}'
};
$.post(
  '{{ route("feed-get-mess", $feed->id) }}',
  myJsonData,
  function(response) {
    $('#AjaxGetMessages').append(response.message);
    console.log(response);
  }
);

The controller is simple:
public function ajax_mess(Request $request)
{
    $feed_ajax = Feed::findOrFail($request->id);
    $messages_ajax = $feed_ajax->messages()->latest()->first();
    return $messages_ajax;
}

But the data is published in the admin panel. It is necessary that during (or after) the publication, my ajax request works and produces the result for everyone on the page where this data is displayed (that is, on another page)

Upd: OR! How to call this request in the controller of another page?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2019-09-03
@deepblack

How to make an AJAX request on one page and get the result on another?

If purely on AJAX, then you need to periodically poll the server for new data.
For example like this:
spoiler
function fetchdata(){
 $.ajax({
  url: 'fetch_details.php',
  type: 'get',
  success: function(response){
   // Perform operation on the return value
   alert(response);
  }
 });
}

$(document).ready(function(){
 setInterval(fetchdata,5000);
});

or
spoiler
function fetchdata(){
 $.ajax({
  url: 'fetch_details.php',
  type: 'get',
  success: function(data){
   // Perform operation on return value
   alert(data);
  },
  complete:function(data){
   setTimeout(fetchdata,5000);
  }
 });
}

$(document).ready(function(){
 setTimeout(fetchdata,5000);
});

Or use Websocket.
This is how Websocket
AJAX works, the response will only come to a request from the page if you make one.
$.post(
  '{{ route("feed-get-mess", $feed->id) }}',
  myJsonData,
  function(response) {
    $('#AjaxGetMessages').append(response.message);
    console.log(response);
  }
);

Are you getting data from the server by POST? o_O
Usually this is done with a GET request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question