【问题标题】:jQuery.post returns 400 bad request error [rails]jQuery.post 返回 400 错误请求错误 [rails]
【发布时间】:2014-08-05 01:34:19
【问题描述】:

我正在使用 rails 4 和 balance.js,并尝试使用 jQuery.post 将返回的令牌发布到后端。我传递了正确的 url,但收到 400 错误。澄清:从 balance 收到有效令牌后,我在 jquery.ajax 上收到错误。

jQuery ->
$('#cc-submit').click (e) ->
    e.preventDefault()

    handleResponse = (response) ->
        if (response.status_code == 201)

            fundingInstrument = (if response.cards? then response.cards[0] else response.bank_accounts[0])

            alert(fundingInstrument.href)

            $('#tablecharge_uri').val(fundingInstrument.href)

            url = '/events/' + urlid + '/tablecharges'

            alert(url)

            jQuery.ajax({type: "POST", url: url, data: {uri: fundingInstrument.href, event_id: urlid}, sucess: (data) ->
                alert data.id
            error: (data) ->
                alert "not cool"})

        else
            alert(response.status + JSON.stringify(response, false, 1))

    payload = {
    name: $('#cc-name').val()
    number: $('#cc-number').val()
    expiration_month: $('#cc-expiration-month').val()
    expiration_year: $('#cc-expiration-year').val()
    ccv: $('#cc-ccv').val()
    }

    balanced.card.create(payload, handleResponse)

这是我的控制器:

class TablechargesController < ApplicationController
def new
    @event = Event.find(params[:event_id])
    @table = @event.tablecharges.build
end

def index
    @event = Event.find(params[:event_id])
    @table = @event.tablecharges.all
end

def create
    @event = Event.find(params[:event_id])
    @table = @event.tablecharges.build(table_charge_params)
    respond_to do |format|
        format.js { render json: 'Your message', status: :ok } 
    end
    if @table.save
        redirect_to @event, :notice => "Thanks for the cash"
    else
        render :new
    end
end

private

def table_charge_params
    params.require(:tablecharge).permit(:name, :email)
end

结束

这是我的模型:

class Tablecharge < ActiveRecord::Base
belongs_to :event
attr_accessor :cc_name
attr_accessor :cc_number
attr_accessor :cc_expiration_month
attr_accessor :cc_expiration_year
attr_accessor :cc_ccv
end

更新: 这是来自 chrome 调试器的完整错误代码:

POST "/events/7/tablecharges" 400 (Bad Request) 
jquery.js?body=1:9632
send jquery.js?body=1:9632
jQuery.extend.ajax jquery.js?body=1:9177
handleResponse tablecharges.js?body=1:13
ret balanced.js:433
window.(anonymous function) balanced.js:395
(anonymous function)

更新 2: 这是来自 Rails 服务器的错误

 ActionController::ParameterMissing - param is missing or the value is empty
 actionpack (4.1.4) lib/action_controller/metal/strong_parameters.rb:183:in `require'

【问题讨论】:

  • 您收到的 400 错误的文本是什么?
  • POST localhost:3000/events/7/tablecharges 400(错误请求)
  • 全文是什么?来自 Balanced 的 400 错误准确地表明了请求中的格式错误。
  • 哦,问题出在 jQuery.ajax 而不是 balance.card.create 中。 balance.js 给了我正确的令牌,我只是无法将其发布到后端?
  • @adminbanks 在url = '/events/' + urlid + '/tablecharges' 我没有看到你的函数中定义的urlid

标签: javascript jquery ruby-on-rails ajax balanced-payments


【解决方案1】:

您的数据负载缺少“table_charge”键

JS

data: {uri: fundingInstrument.href, event_id: urlid}
...

铁轨

def table_charge_params
    params.require(:tablecharge).permit(:name, :email)
end

require 在这里的描述性非常好,如果没有密钥,它就会爆炸

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-28
    • 2018-04-07
    • 1970-01-01
    • 2019-07-11
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多