Amagi's avatar
Level 1

Change content with AJAX without reloading page

Hello, I am new to Laravel and i'm making an application for a radio so they have a radio player in the header but everytime I route to another page it also reloads the radioplayer so i want to use AJAX to only reload the content to keep the radioplayer intact without reloading it. But now i don't have any experience in AJAX so my question is what are the steps to accomplish this problem? I want to use AJAX with Javascript on a tag. I tried to find many topics on forums but they are not what i'm looking for.

Amagi's avatar
Level 1

@Tray2 Thanks for your advice, it's not that i know JavaScript but i don't know how to load each content page in the url part of AJAX

Amagi's avatar
Level 1

@Tray2 for example

const response = await fetch('/my/url');

if (!response.ok) {
}

const body = await response.text()

he expects an url but what do i fill to load all my routes? like Homepage - About us - Radio - Contact

Tray2's avatar

Like I said, using Livewire would probably be the best solution, since it does exaclty whar you need out of the box (more or less).

Doing it yourself yo need to fetch the html from the server, either using API routes of the ones you already have with a bit of modification of course.

I would use the fetch api.

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

To create a function for it.

document.querySelector('#swappable-content').innerHTML = await getRequest(page).
1 like

Please or to participate in this conversation.