包含参数时,Axios发布请求无需参数即可工作

时间:2019-01-19 12:46:07

标签: ruby-on-rails vuejs2 ruby-on-rails-5 active-model-serializers grape

我正在使用Vue.js 2构建Rails 5.2.1应用程序。我已将后端与前端集成在一起,构建了API,但是当我尝试通过axios向我的后端发送POST请求时出现问题。我有一个v1/users/me/create葡萄端点,如下所示:

# frozen_string_literal: true

module API
  module V1
    module Users
      module Me
        class Create < Base
          namespace :create do
            desc "Create a user"
            params do
              optional :email, type: String, desc: "User's email address"
              optional :first_name, type: String, desc: "User's first name"
              optional :last_name, type: String, desc: "User's last name"
              optional :password, type: String, desc: "User's password"
              optional :password_confirmation, type: String, desc: "User's password confirmation"
            end
            post do
              # user will be created here
            end
          end
        end
      end
    end
  end
end

我想通过axios发送POST请求,代码如下:

export default {
  name: 'SignUp',
  data () {
    return {
      email: '',
      error: false,
      first_name: '',
      last_name: '',
      password: '',
      password_confirmation: '',
      buttonText: 'Sign Up'
    }
  },
  methods: {
    signUp () {
      this.buttonText = 'Loading...'
      let data = { email: this.email, first_name: this.first_name, last_name: this.last_name, password: this.password, password_confirmation: this.password_confirmation }
      this.$http.post('/users/me/create', data)
        .then(response => {
          // do something
        })
        .catch(response => {
          // do something
        })
    }
  }
}

当我发送不带参数的请求(data变量)时,请求通过,我可以在后端访问它-当然不带参数。但是,当我使用参数发送它时,它立即进入catch块,后端返回以下错误:

Started POST "/api/v1/users/me/create" for 127.0.0.1 at 2019-01-19 13:40:56 +0100
   (1.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
  ↳ /Users/lszmelc/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98

ArgumentError (wrong number of arguments (given 5, expected 2)):

grape-active_model_serializers (1.3.2) lib/grape-active_model_serializers/formatter.rb:5:in `call'

这可能是什么原因?

0 个答案:

没有答案