Ajax调用返回HTML页面作为响应而不是呈现页面(Rails)

时间:2018-05-29 22:19:01

标签: javascript ruby-on-rails angularjs ajax render

嘿有一个.erb文件的渲染问题,在我的AJAX调用中,我在rails上调用我的创建操作,我在其中验证并处理表单数据并将完成的订单数据作为渲染发送回来:json工作正常。

我有一个条件,检查参数是否存在,如果已经完成的订单数据通过渲染作为响应传回:json

如果不存在,它将呈现收据页面。

问题是当我呈现收据页面时,完整的HTML收据页面作为响应返回而不是呈现页面。请帮助!

$scope.placeOrder = function() {
        var body = composeOrderBody();
        var isValid = validateForm(body.order);

        if(isValid) {
          var orderComplete = '<%= @orderComplete %>';
          var baseUrl = '<%= request.base_url %>';

          console.log('Passing order object: ', body.order);


          $http({
              method: 'POST',
              headers: {
                'Content-Type': 'application/json'
              },
              url: checkout_url,
              data: {
                order: body.order,
                xhr_request: true
              },
            }).then((function(_this) {
              return function(response) {

              if(typeof response.data == 'undefined' || response.data == null || !response.data) {
                 console.log('Error: missing Order Number from Order Confirmation data.', response.data);
              }

              console.log('Order Confirmation response data object:' , response.data);

              if(orderComplete) {
                var redirectUrl = 'http://' + orderComplete
                var order_params = `?oid=${response.data.oid}?cart=${response.data.cart}?total=${response.data.total}`
                window.location.href = redirectUrl + order_params;
              } else {
                console.log('Base Url: ', baseUrl);

                // window.location.href = `${baseUrl}/receipt`;

              }

              };
            })(this));


        } else {
          console.log('Form Validation or Stripe Validation Failed');
        }

    } // end placeOrder

Rails代码

# Redirect to orderComplete URL if it's set
  if !@orderComplete.blank?
      puts 'orderComplete parameter is not blank'
    # Sum up all the line item quantities
    qty = @order.line_items.inject(0) {|sum, line_item| sum + line_item.quantity}

    # Get all of the coupons (and values) into a string
    coupons = @order.applied_coupons.map { |coupon| coupon.coupon }.join(',')
    coupon_values = @order.applied_coupons.map { |coupon| '%.2f' % coupon.applied_value.to_f }.join(',')

    order_params = {
      "oid" => URI::escape(@order.number),
      "cart" => URI::escape(@cart),
      "total" => URI::escape('%.2f' % @order.total),
    }

    @redirectUrl = URI.parse(URI.escape(@orderComplete))
    @redirectUrl.query = [@redirectUrl.query, order_params.to_query].compact.join('&')
    @redirectUrl = @redirectUrl.to_s

    if params[:xhr_request]

     render json: order_params.to_json

     return

    end

    render 'receipt_redirect', :layout => 'receipt_redirect'
  else
      puts 'OrderComplete Parameter is blank'

     render 'receipt', :layout => 'receipt', :campaign => @campaign
  end

0 个答案:

没有答案