-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
W3C HTML JSON form submission #1667
Comments
It's a lot of added complexity for little value. And there is no implementer interest because of that. |
If I want to convert a form into an object, I may implement a simple solution... something like // convert the form into an object
var formObj = {};
for (var i = 0; i < form.length; i++) {
if (form[i].id) {
formObj[form[i].id] = form[i].value;
}
} @annevk, thanks for the information. |
You can also use FormData(form). Perhaps we should expose a toJSON on FormData. That might be fairly reasonable. |
Hmm... sounds good. form.addEventListener('submit', e => {
e.preventDefault();
var formObj = {};
var fd = new FormData(e.target);
for (var item of fd) {
formObj[item[0]] = item[1];
}
fetch(`/api/category/${formObj.id}`, {
method: 'put',
headers: new Headers({
'Content-Type': 'application/json'
}),
body: JSON.stringify(formObj)
});
}); Let me dream a bit, please... form.addEventListener('submit', e => {
e.preventDefault();
var fd = new FormData(e.target).toJSON();
fetch(`/api/category/${formObj.id}`, {
method: 'put',
body: fd // then the fetch process should guess the content-type
});
}); Or just by setting the content-type form.addEventListener('submit', e => {
e.preventDefault();
var fd = new FormData(e.target);
fetch(`/api/category/${formObj.id}`, {
method: 'put',
headers: new Headers({
'Content-Type': 'application/json'
}),
body: fd // the fetch process will call toJSON automatically
});
}); The main goal that I see here is the possibility to expose the form depending on the content-type. |
@annevk do you want to move this to XHR to add a toJSON() and close this issue? |
I opened whatwg/xhr#84 though given your comments in whatwg/url#143 I wonder whether returning a JSON object here is the way to go. |
@rianby64 if you're satisfied with this resolution please close this issue. Your last idea goes a tad too far I think. We don't really want browsers to inspect the headers to determine how to serialize a value. That's too much action-at-a-distance. |
So, the first variant seems to be better than the second one. Nice 😄 ! Thanks a million! |
Coming a bit late (6 month) to this topic (just saw it), but would like to share my view on JSON, form URL encoded, and plain text JSON encoding is more or less like the "x-www-form-urlencoded" format. It is not designed to embed binary data. As such, both the application/x-www-form-urlencoded serializer and the text/plain encoding algorythm says that they serialize "file" entries in form data by replacing their value with their file's name only. Base64 encoding has been estimated as a no go for files use case (to much overhead). So I think it would be fair to says that either native XHR/fetch support or form data toJSON() method should behave the same. What for? Well
Multipart, form-data, and JSON The way we send files over So if we would like to send Form entries including files using JSON encoding, the only approach I would imagine is defining a In The main "part header" is The second main "part header" is In a JSON flavor, "non-file" parts could have their " What for?
That's all... Just wanted to share what kind of bell this feature request did ring to me. Still, I agree, server side frameworks usually work pretty well with classic form data... But well, getting automatically the right value type would be an extra I often felt was missing.
|
I also think it would be useful to make <form method="post" action="https://httpbin/post" enctype="application/json">
<input name="foo" value="bar">
<input type="submit">
</form> https://jsfiddle.net/crl/cj9f0qna/ |
The problem is that any on-the-wire extension to forms requires navigation to become CORS-aware, which would be rather involved and with lots of details to think through. Coupled with lack of active implementer interest it doesn't really seem like something that's going to happen anytime soon. Perhaps once navigation is refactored better so that it's more clear what the consequences would be. |
for the record @rianby64, just do: fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(Object.fromEntries(new FormData(form)))
}); |
Hello WHATWG! I'm reading the section for enctype and found that there's no state for application/json or something similar that allows form submission in JSON.
I ignore the reasons that forced W3C to stop maintaining that specification. But I'm wondering if that spec worths or not?
Thanks for your time.
The text was updated successfully, but these errors were encountered: