Javascript jQuery.post返回400错误请求错误[rails]
我正在使用rails 4和balanced.js,并试图使用jQuery.post将返回的令牌发布到后端。我通过了正确的url,但收到400错误。澄清一下:在收到来自balanced的有效令牌后,我在jquery.ajax上遇到了一个错误Javascript jQuery.post返回400错误请求错误[rails],javascript,jquery,ruby-on-rails,ajax,balanced-payments,Javascript,Jquery,Ruby On Rails,Ajax,Balanced Payments,我正在使用rails 4和balanced.js,并试图使用jQuery.post将返回的令牌发布到后端。我通过了正确的url,但收到400错误。澄清一下:在收到来自balanced的有效令牌后,我在jquery.ajax上遇到了一个错误 jQuery -> $('#cc-submit').click (e) -> e.preventDefault() handleResponse = (response) -> if (response.st
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
更新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'
您的数据有效负载缺少键“table_charge” JS 轨道
require在这里是非常描述性的,如果没有键,它就会爆炸您收到的400错误的文本是什么?POST 400(错误请求)全文是什么?来自Balanced的400个错误正好表明请求中的格式错误。哦,问题出在jQuery.ajax中,而不是balance.card.create中。balanced.js给了我正确的令牌,我只是无法将其发布到后端?@adminbanks In
url='/events/'+urlid+'/tablecharges'
我看不到您的函数中定义了urlid
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)
ActionController::ParameterMissing - param is missing or the value is empty
actionpack (4.1.4) lib/action_controller/metal/strong_parameters.rb:183:in `require'
data: {uri: fundingInstrument.href, event_id: urlid}
...
def table_charge_params
params.require(:tablecharge).permit(:name, :email)
end