Skip to content
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

Why does my form always get submitted with Content-Type:application/x-www-form-urlencoded; #506

Open
JohnSmall opened this issue Feb 5, 2018 · 4 comments

Comments

@JohnSmall
Copy link

I'm trying to send a form with Content-Type:application/json; I set

`<form data-remote="true" data-type="json">...</form>`

As it says in the documentation.

But it's always sent as Content-Type:application/x-www-form-urlencoded;

Is this a bug or is there a special undocumented gotcha such that setting data-type='json' doesn't actually send json encoded data.

@jeremy
Copy link
Member

jeremy commented Feb 5, 2018

What are you seeing when you step through in the debugger?

@l4cr0ss
Copy link

l4cr0ss commented Apr 19, 2019

I have the same problem. With data-remote: true my form is still being submitted as 'Content-Type': 'application/x-www-form-urlencoded'.

See this example repl.it that illustrates the problem. Try and submit the form without any input and notice that www-form-urlencoded is submitted and HTML is returned, even though form has data-remote: true set.

https://repl.it/repls/TriangularEmptyWireframes

@l4cr0ss
Copy link

l4cr0ss commented Apr 19, 2019

It looks like this could be related to rails/rails#20610. Adding a format.js to the respond block fixes the return type issue (it returns JSON instead of HTML). However, the rails-ujs parser still breaks because it's expecting HTML.

@l4cr0ss
Copy link

l4cr0ss commented Apr 19, 2019

Setting data type to json fixes the issue, like form_with(..., data: { type: 'json' }) do |form|..

Doing this means you don't have to create a format.js response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants