Reputation: 7441
I'm probably missing something here. I've read the SvelteKit core concepts documentation here - https://kit.svelte.dev/docs that covers, routing, loading data and form actions. However, I can't see an obvious way to simply call a function from a button on a Svelte page, pass it something like "hello" and then have a function on the server respond with something like "world".
I see there's some magical non javascript ways to do this with forms, and a way to load data from a database.
Are there any working examples that show how to wire something like a hello world together ?
Upvotes: 4
Views: 11223
Reputation: 2929
I think you are missing the point here.
Server functions meant to be used on the server-side, you shouldn't be able to access it from client side. You can't trigger server function from client. SS is running on the server. Client side is running in the user's browser.
If you desire sending a message to server and get a response, for that you gotta send a request (POST, GET ...) (explained below)
Everything is built on JS (TS) here, I'm not sure what do you mean by without JS.
If you like to trigger a server function and get a response, you have various options. You can create a folder called /api/ then add your endpoint folders here each folder should have a +server.js
Other option is creating a self contained end point, in your regular path folders you can create a +page.server.js file, then handle requests here.
You can use fetch method with JS, or just post post your form. But using fetch will provide you some nice cool functionality.
Various examples can be found on youtube. I'm not sure if it's ok to share youtube links here. So go make a search.
Upvotes: 5
Reputation: 7441
As per @Corri the answer to this question is documented here: kit.svelte.dev/docs/routing#server-receiving-data
Upvotes: 4