Just another AJAX in k3 question

Hello,

I am a tad confused about ajax in K3.

Use case is a website which loads part of its content dynamically, while retaining other content unless there is a reload. The content in question would be one of several snippets, each fetching field content from actual and other pages, and possibly containing other snippets.

Being this a sort of SPA, would the API be the way to go? Then, as there is no user login, how to solve authentication? should I create a specific user for this and use basich https auth with it ?

Still I can’t see clearly how to deal with the API in my ase.

With k2 I had an api template with something like:

   ....
    if(kirby()->request()->ajax()) {
    	$data[] = snippet($_POST["snippet"], array('p' => $_POST["page"] ), true);	
    	echo json_encode($data);
    }
    ...

…and I POSTed the snippet and page vars to that template, then placed the response data directly to the page with the likes of:

$(container).html(data);

But I see request ajax is deprecated and the api is built-in etc…

If this is already well explained, please direct me to the right docs, as I could not find them.

Thank you

I‘d use content representations
https://getkirby.com/docs/guide/templates/content-representations instead of an if statement that checks if the request is an ajax request

Thank you, but how to deal with snippets?

Your content representation can return a snippet.

As additional info: Your content representation can have its own controller.