简体   繁体   English

在Rails中构建自定义ajax表单时,要发送给控制器以使其响应Json的正确数据是什么?

[英]When building a custom ajax form in Rails, what is the proper data to send to to the controller to get it to respond to Json?

I want to build a custom ajax form (not interested in using the Rails out of the box solution): 我想构建一个自定义的ajax表单(对使用Rails开箱即用的解决方案不感兴趣):

    $('form.ajaxForm').submit(function(e){
        e.preventDefault();
        var $form = $(this),
          formValues = getFormValues($form),
          formUrl = $form.attr('action');


        $.post(formUrl, {
          data : formValues,
          contentType: "application/json",
          success : function(data, textStatus, jqXHR){
             // do success here
          }
        });
      });

var getFormValues = function($form){
    var values = {};
    var valuesArray = $form.serializeArray();
    for(var i = 0, max = valuesArray.length; i < max; i++){
      var value = valuesArray[i];
      values[value.name] = value.value
    };
    return values;
  };

I do want to use Rails 3's ability to respond differently when it receives a json request such as: 我确实想利用Rails 3的能力在收到json请求时做出不同的响应,例如:

def create
    @registration = Registration.new(params[:registration])

    respond_to do |format|
      format.html { render action: "new" }
      format.json { render json: @registration.errors, status: :unprocessable_entity }
    end
  end

For some reason, Rails cant process my request during the post, and doesn't respond to json. 由于某些原因,Rails无法在发布期间处理我的请求,并且不响应json。 What in the http headers tells it to respond to json, and what is the proper way to post this data? http标头中的内容告诉它响应json,并且发布此数据的正确方法是什么?

Looking at the Rails API docs I came across this tidbit: 在查看Rails API文档时,我遇到了这个提示:

Rails determines the desired response format from the HTTP Accept header submitted by the client. Rails从客户端提交的HTTP Accept标头中确定所需的响应格式。

jQuery's $.ajax call has an accepts setting that can be used to set the value for this header but I believe that using dataType will automatically configure this for you. jQuery的$.ajax调用具有一个accepts设置,可用于设置此标头的值,但我相信使用dataType会自动为您配置此值。

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

相关问题 Rails Controller-“响应格式” html和json的目的是什么? - Rails Controller - What is the purpose of “respond to do format” html and json? 使用控制器操作响应JSON的Rails方式(在3.2.6中)是什么? - What is the Rails Way (in 3.2.6) to respond to JSON with controller actions? 在rails中创建JSON对象,然后使用jQuery / Ajax从操作中获取JSON的正确方法是什么? - What is the proper way to create a JSON object in rails, and then use jQuery/Ajax to get the JSON from an action? 在Rails中使用Ajax调用时,从控制器传递数据到视图的正确方法 - Proper way to pass data from controller to view when using ajax calls in Rails 在控制器导轨中获取表单数据 - Get form data in controller rails Rails 3:在rails中使用JSON响应REST-ful操作的正确方法是什么? - Rails 3: What is the proper way to respond to REST-ful actions with JSON in rails? 在Rails中获取Highcharts数据的正确方法是什么 - What is the proper way to get the data for Highcharts in Rails Rails 4 Ajax控制器的默认response_to - Rails 4 default respond_to for ajax controller 如何在Rails控制器中响应AJAX POST请求 - How to respond to AJAX POST request in rails controller Rails使用AJAX参数发送到视图中的控制器中的自定义操作 - Rails Send with AJAX parameters to custom action in controller from the view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM