Ajax url参数未正确附加 - Rails 4

时间:2015-09-14 13:45:54

标签: javascript jquery ruby-on-rails ajax

我正在尝试使用jquery向我的rails发出POST请求。

我的ajax调用值

$('input[type=submit]').unbind().bind('click', function() {
    // extract category id from hidden field
    var category = $("#category_id").val();
    var category_id = category.split("=>").pop(-1);
    category_id = category_id.replace("}","");
    // extract question id from hidden field
    var question = $("#question_id").val();
    var question_id = question.split("=>").pop(-1);
    question_id = question_id.replace("}","");

    // init new array to pass choices
    var checked = [];
    $("input:checked").each(function() {
      var whole_id = $(this).attr("id");
      var id = whole_id.split("_").pop(-1);
      checked.push(id);
    });

    var answers = JSON.stringify(checked);
    console.log(checked);
    retrieve(answers,category_id,question_id);
  });

ajax电话

function retrieve(answers,category,question) {

 $.ajax({
      cache: false,
      type: "POST",
      url: "/categories/" + category + "/questions/" + question + "/retrieve",
      data: answers,
      success: function(data) {

      },
      error: function(xhr) {
        alert("The error code is: " + xhr.statusText);
      }
    });
  };

路由

resources :categories do
    get '/random_question', action: 'random_question', controller: 'categories', as: 'random_question'

    resources :questions do
      post '/retrieve', to: 'questions#retrieve'

      resources :choices, only: [:index]
    end
  end

控制器

def retrieve
    @category = Category.find_by(params[:category_id])
    respond_to do |format|
      format.html { redirect_to category_questions_url, notice: 'success'  }
      format.json {
        render json: {
          message: "success"
          }
        }
    end
  end

我收到404错误(未找到),网址就像这样

http://localhost:3000/categories/%222%22/questions 404 (Not Found)

这是错误的。

也在chrome dev工具的网络标签中我得到了这个

ActiveRecord::RecordNotFound in QuestionsController#index

Couldn't find Category with 'id'="2"

2 个答案:

答案 0 :(得分:1)

注意到您将category_id和question_id作为字符串发送,而不是数字。而不是棘手的黑客做:

// extract category id from hidden field
var category = $("#category_id").val();
var category_id = parseInt(category);
// extract question id from hidden field
var question = $("#question_id").val();
var question_id = parseInt(question);

它应该是金色的。在控制台试了一下,它仍然返回一个字符串,这是你的问题

答案 1 :(得分:1)

正如用户TomD最初建议的那样,我得到字符串,我需要将它们转换为整数。

但真正的问题是隐藏的输入值属性有<toolBarContributions xmi:id="_iJ5CQFxFEeWnG7SUWOY3vg" elementId="com.ami.veb.application.toolbarcontribution.0"> <children xsi:type="menu:ToolControl" xmi:id="_lvnLwFxFEeWnG7SUWOY3vg" elementId="com.ami.veb.application.toolcontrol.0" contributionURI="platform:/plugin/org.eclipse.e4.ui.workbench.addons.swt/org.eclipse.e4.ui.workbench.addons.perspectiveswitcher.PerspectiveSwitcher"/> </toolBarContributions> 而不只是<LinearLayout .... > <ExpandableListView .... > </ExpandableListView> </LinearLayout>

在我的隐藏输入字段上执行以下操作后,所有工作都像TomD所说的那样。

:value=>1