Use page's actions without a from | submit programmatic/imperative form #13502
-
Hello everyone. It's not clear what to do when we want to use the page actions but we do not have a form. The docs suggest to expose a JSON API, but I would prefer to keep my server side pattern consistent and keep the easy access to the This is the closest I've got so far: // +page.svelte
const triggerSubmit = () => {
const formData = new FormData();
formData.append('myField', JSON.stringify({myValue: "value"}));
fetch(page.url.href + '?/myAction', {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data'
},
body: formData
});
} The action seems to be triggered (because if the name
I was wandering:
P.S. P.P.S |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
I really do not recommend using actions like this. They return data specifically to be handled internally by SvelteKit. There are ways of handling the response manually (see docs here), but this will probably just unnecessarily complicate things.
The methods in The code can by the way be fixed by removing the A correctly generated header looks like this:
The big question is why you need to do any of that in the first place, though. You can e.g. use the callback of the What the best approach is really depends on the specifics of what you are actually trying to do here. |
Beta Was this translation helpful? Give feedback.
-
Hi @brunnerh. Thank you for all your information. My use-case is when you have no I was maybe mislead in thinking that Actions were the de facto patter to use to structure the request-response flow in sveltekit pages. Followup question: Using the It feels weird to have two patterns for sending/receiving requests and reacting to responses just because at some point the data may or may not touch a form in the UI. Thanks again. |
Beta Was this translation helpful? Give feedback.
-
Fun aside. In the meantime I came up with this ungodly monstrosity, which is definitely not the right approach. What I find funny though is that it works like a charm and it behaves exactly like a form submission with sveltekit SSR. export const fakeFormSubmission = (action: string, fieldName: string, fieldValue: any) => {
const form = document.createElement('form');
form.method = 'POST';
form.action = action;
form.style.display = 'none';
const input = document.createElement('input');
input.type = 'text';
input.name = fieldName;
input.value = JSON.stringify(fieldValue);
form.appendChild(input);
document.body.appendChild(form);
form.submit();
setTimeout(() => {
form.remove();
}, 500);
}; fakeFormSubmission('?/myAction', 'myField', myContent); |
Beta Was this translation helpful? Give feedback.
I really do not recommend using actions like this. They return data specifically to be handled internally by SvelteKit. There are ways of handling the response manually (see docs here), but this will probably just unnecessarily complicate things.
The methods in
+server
likePOST
also receive aRequestEvent
with all those things.The code can by the way be fixed by removing the
headers
because otherwise theboundary
will be missing. Browsers will automatically set the …