首页 > 解决方案 > 对 Rails 的 AJAX 请求省略了右花括号

问题描述

我在这个 repo上找到了一个托管在 heroku 上的应用程序(提供完整源代码)。

当我执行 post 或 put 请求时,缺少右花括号。例如,此处找到的代码执行发布以创建食谱。

this.axios.post('recipes', { recipe: resource });

我在网络工具中看到以下原始请求。

{"recipe":{"ingredients":"","name":"t","tags":[],"categories":[]}}

但是服务器端我看到了这个错误。

{"recipe":{"ingredients":"","name":"test","tags":[],"categories":[]

ActionDispatch::Http::Parameters::ParseError (765: unexpected token at '{"recipe":{"ingredients":"","name":"test","tags":[],"categories":[]'):

rails/actionpack/lib/action_dispatch/http/parameters.rb:117: in `rescue in parse_formatted_parameters'
rails/actionpack/lib/action_dispatch/http/parameters.rb:111:in `parse_formatted_parameters'
rails/actionpack/lib/action_dispatch/http/request.rb:381:in `block in POST'

就好像最后两个花括号被切断了一样。我一路跳进去ActionDispatch::Request

> get_header "RAW_POST_DATA"
=> "\r\n" + "{\"recipe\":{\"ingredients\":\"\",\"name\":\"t\",\"tags\":[],\"categories\":[]"

我不知道它是来自 axios 侧还是 rails 侧。由于开发工具显示了一个有效的请求,我假设它在 Rails 一侧,但我不知道是什么原因造成的。

它适用于heroku,但不适用于开发环境,我想不出有什么改变。要重现,您可以像任何其他 Rails 应用程序一样设置并为用户播种并尝试创建带有标题的食谱。

标签: ruby-on-railsajaxvue.jsruby-on-rails-5axios

解决方案


推荐阅读