在我的Rails应用程序中,我正在发出一个ajax请求,作为 $ .ajaxSetup({/ get_paypal_button' });在Rails应用程序中给出错误的Ajax请求
var dataToSend = { fundraiser_id: <%= @fundraiser.id %>,
amount: $("#amount").val(),
currency: $('#donor_donation_currency').val(),
comment: $('#donor_donation_comment').val(),
anonymous: $("#donor_donation_anonymous").is(":checked"),
donor_email: $('#email').val(),
donor_name: $('#name').val(),
donor_city: $('#city').val(),
donor_country: $('#country').val() };
$.ajax({
type: "POST",
dataType:'JSON',
async: true,
data:dataToSend,
success: function(json){
$('#id_amount').val(json.amount);
$('#custom_content').val(json.custom);
},
error:function (xhr, ajaxOptions, thrownError){
console.dir(xhr);
console.dir(thrownError);
}
});
在控制器我处理的数据作为
def get_paypal_button
@fundraiser = Fundraiser.where(:id => params[:fundraiser_id]).last
if params[:currency] == 'INR'
amount_in_sgd = params[:amount].to_f/@fundraiser.sgd_to_inr.to_f
elsif params[:currency] == 'USD'
amount_in_sgd = params[:amount].to_f*@fundraiser.usd_to_sgd.to_f
else
amount_in_sgd = params[:amount].to_f
end
puts '----------finding donor-------------'
@donor = Donor.find_or_initialize_by_email(params[:donor_email])
@donor.update_attributes(:name => params[:donor_name], :city => params[:donor_city], :country => params[:donor_country])
custom = @donor.id.to_s + "::" + params[:anonymous].to_s + "::" + params[:comment].to_s
@hash = {:amount => amount_in_sgd, :custom => custom}.to_json
respond_with(@hash) do |format|
format.json { render :json => @hash }
end
末
当我使用调试器,并得到@hash它给出的值(这是正确的)
"{\"amount\":15.0,\"custom\":\"2::false::\"}"
但ajax请求正在执行我编写的代码以防出错,并且它获得空响应。
我已经尝试了一切,完全无能为力,因为类似的代码在几天前工作。
我看到你在你的散列上使用'.to_json' - 你不需要调用它。 – MikDiet 2012-04-15 16:56:51
我也试过,但仍然得到空的回应:( – rtcoms 2012-04-15 17:00:32
它会帮助在数据类型中使用小写json? – DGM 2012-04-15 17:14:50