jQuery.Post返回400错误请求错误[rails]
jQuery.post returns 400 bad request error [rails]
我正在使用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在这里很有描述性,如果键不存在,它会爆炸
相关文章:
- Rails第一个应用程序错误——Errno::ENOENT
- 为什么Highchart在我的rails应用程序中加载了两次?(未捕获的Highcharts错误#16)
- Angular JS/Rube-on-Rails-控制器未识别错误
- 在nginx错误495(错误的客户端证书)上提供Rails资产管道中的样式表
- Class.create()上的Comatose(rails插件)javascript错误
- Rails 应用程序(狂欢) - 突然的“未捕获的类型错误”
- websocket rails,websocket握手错误
- Ruby on Rails屏幕抓取错误-“;您必须在浏览器中启用javascript才能使用此应用程序"
- Rails render_to给出406不可接受的错误
- Rails和ajax文件上传-无法读取属性'innerHTML'的空错误
- Rails应用程序中的角度给定错误
- 试图从错误位置加载资源的Rails
- 大视频.js在 Rails 路由错误中
- 如何在 Rails 中呈现 json 表单的错误消息
- Rails:为 JavaScript 中的每个大括号抛出语法错误
- Rails 4 - 没有JavaScript错误,但JS不起作用
- Rails JavaScript JSON 解析错误
- Rails 远程表单抛出语法错误:意外的标记:
- 为什么我的 rails AJAX 请求会抛出内部服务器错误
- jQuery.Post返回400错误请求错误[rails]