jQuery.Post返回400错误请求错误[rails]

jQuery.post returns 400 bad request error [rails]

本文关键字:错误 rails 请求 jQuery 返回 Post      更新时间:2023-09-26

我正在使用rails 4和balanced.js,并试图使用jQuery.post将返回的令牌发布到后端。我有正确的url通过,但收到一个400错误。澄清:我得到一个错误的jquery。从balanced.

接收到有效令牌后的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 server

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

您的数据有效负载缺少键"table_charge"

JS

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

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

require在这里很有描述性,如果键不存在,它会爆炸