【发布时间】:2017-08-25 22:11:13
【问题描述】:
我从一个 JS 应用发送:
let config = {
method: 'POST',
headers: { 'Content-Type':'application/x-www-form-urlencoded' },
body: `email=${creds.email}&password=${creds.password}`
}
fetch('http://localhost:3000/sessions', config)
在我的 Rails 控制器中:
class SessionsController < ApplicationController
def create
@user = User.where(email: params[:email]) # this doesn't work!
end
end
我收到 400 错误请求:
Started POST "/sessions" for 127.0.0.1 at 2017-08-25 23:56:56 +0200
Error occurred while parsing request parameters.
Contents:
email=hello&password=moto
ActionDispatch::Http::Parameters::ParseError
(743: unexpected token at 'email=hello&password=moto'):
任何人都可以了解如何处理来自 JS 的这些数据?
【问题讨论】:
-
很难判断可能发生了什么,因为问题中的 JS 代码似乎与导致问题日志消息中显示的问题的 JS 代码不同。日志消息显示 POST 到
/sessions路由的日志,但您的 JS 代码显示 POST 到/sessions/create。日志消息还显示它正在接收email=hello&password=moto,但您的 JS 代码是用于以username=hello&password=moto形式发送参数的 POST。 -
鉴于问题中显示的 JS 代码与显示的服务器端日志消息的详细信息不匹配,可能希望使用stackoverflow.com/posts/45890012/edit 编辑/更新您的问题并发布实际的确切信息您正在测试请求的 JS 代码,以及当服务器接收到该请求时您在服务器端获得的实际确切日志消息。
-
@sideshowbarker 对不起!你说的对。不好的复制粘贴。我现在已经编辑了问题以完全匹配我在这里看到的内容。这是
POST到/sessions,发送/接收的数据是email=hello&password=moto
标签: javascript ruby-on-rails redux form-data