简体   繁体   English

Ajax网址参数未正确附加-Rails 4

[英]Ajax url parameters not appended correctly - Rails 4

I am trying to make a POST request to my rails using jquery. 我正在尝试使用jquery向我的Rails发出POST请求。

my values for ajax call 我对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 call 阿贾克斯电话

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);
      }
    });
  };

routing 路由

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

controller 控制者

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

i get a 404 error(not found) and the url is like this 我收到404错误(未找到),并且网址是这样的

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

which is wrong. 这是错误的。

also in network tab of chrome dev tools i get this 也在chrome dev工具的网络选项卡中我得到了

ActiveRecord::RecordNotFound in QuestionsController#index

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

Just noticed that your sending category_id and question_id as a string, not a number. 只是注意到您发送的category_id和question_id是字符串,而不是数字。 Instead of the tricky hacks do: 代替棘手的黑客:

// 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);

And it should be golden. 它应该是金色的。 Tried your way in the console and it still returning a string, there's your problem 在控制台中尝试了一下方式,但仍然返回了字符串,这是您的问题

As user TomD originally suggested is that i get strings and i need to convert them to integers. 正如用户TomD最初建议的那样,我得到了字符串,并且需要将它们转换为整数。

But the real problem was that the hidden input value attribute had :value=>1 instead of just 1 但是真正的问题是隐藏的输入值属性具有:value=>1而不是仅1

after doing the following on my hidden input fields everything worked like TomD said to do. 在隐藏的输入字段上执行以下操作之后,所有操作均与TomD所说的一样。

 <%= hidden_field_tag :category_id,params[:category_id], value: params[:category_id] %>
 <%= hidden_field_tag :question_id,params[:id], value: params[:id] %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM