Ajax请求的Javascript(语法错误:非法字符)

时间:2015-10-12 07:27:25

标签: javascript ruby-on-rails ajax renderpartial

在我看来,我使用ajax将表格渲染为部分视图(Ruby on Rails):

j.ajax({
  type: 'POST' ,
  url: '<%=  url_for :action => "distribute_payments" %>',
  data : {
    'no_payments' :n,
    'total_payment' :t,
    'id' :id,
  },
  beforeSend : function() {
  },
  success : function() {
    j("#payments_distribute").html(data);
  }
});

这很有效:

This is the view I want to show

但现在我需要在局部视图中运行以下javascript:

  function payment_sum() {
    var precision = parseInt("<%= @precision %>");
    var sum = 0;
    j('.payment_box').each(function () {
      sum += +parseFloat(this.value) || 0
    });

    return sum.toFixed(precision);
  }

  function submit(){
    alert ('I am here');
    var total = "<%= @total_payment %>";
    var n= "<%@no_payments%>";
    if (payment_sum()!= total){

      alert('<%=t('cant_pay_more')%>');
      j('.payment_box').each(function () {
      this.value= total/n });

      return false;
    } 
    else
      return true;

}

但是当我点击按钮时,javascript代码不会运行。所以我打开了inspect元素并发现了这个错误:

ReferenceError: data is not defined


j("#payments_distribute").html(data);

我在这里修好了:

j.ajax({
  type: 'POST' ,
  url: '<%=  url_for :action => "distribute_payments" %>',
  data : {
    'no_payments' :n,
    'total_payment' :t,
    'id' :id,
  },
  beforeSend : function() {
  },
  success : function(data) {
    j("#payments_distribute").html(data);
  }
});
  }

但这是结果:

enter image description here

我有这个错误:

SyntaxError: illegal character


\r\n    \r\n  function payment_sum() {\r\n    var precision

当我点击保存时,我收到一个错误,因为那是我的参数的返回方式:

Parameters: {"\\\"payments"=>{"1"=>{"title"=>{"\\\""=>"\\\"Second"}, "date"=>{"\\\""=>"\\\"2015-11-12\\\""}, "amount"=>{"\\\""=>"\\\"1600000.00\\\""}}, "0"=>{"title"=>{"\\\""=>"\\\"First"}, "date"=>{"\\\""=>"\\\"2015-10-12\\\""}, "amount"=>{"\\\""=>"\\\"1600000.00\\\""}}}, "\\\"authenticity_token\\\""=>"\\\"30wp5EYedFmam9BQR8f9Qu2rn/rWCx0HNW0WT968C1o=\\\"", "\\\"\\\""=>"\\\"12"}

ActionController::RoutingError (No route matches "/parent_wise_fee_payments/manage_payments/\\\"/parent_wise_fee_payments/manage_payments/10" with {:domain=>"acme.local", :subdomain=>"acmeschool", :method=>:get, :host=>"acmeschool.acme.local"}):

我无法弄清楚这里的错误是什么,为什么在论证&#34;数据&#34;在我的成功功能失踪了我仍然得到了我需要的结果,而无法运行另一个javascript函数?你能帮帮我吗?

0 个答案:

没有答案
相关问题