简体   繁体   中英

Sending a JSON Object via AJAX POST Request to Rails Controller

I thought this would be straightforward since I constantly use AJAX to update models via an actual form that exists in the DOM but when trying to send a JSON object I am running into problems trying to get it to work.

I know there are a lot of similar questions but none seem to be concrete in terms of what the JavaScript should be and how the request should be parsed by the controller, I have tried every variation I can think of but I still can't retrieve the data from the request.

JS

var form_data = JSON.stringify(quote);

$.ajax({
    url: '/quotes/save_draft',
    data: { params: form_data },
    type: 'POST',
    contentType: 'application/json',
    cache: false,
    success: function(data) {

    },
    error: function() {

    }
});

For the data attribute I have tried

data: { params: form_data },
data: form_data,
data: quote,

Rails Controller

RAILS_DEFAULT_LOGGER.info params.inspect
RAILS_DEFAULT_LOGGER.info params[:_json].inspect

but I still can't get a property of the original JSON object to print successfully, eg RAILS_DEFAULT_LOGGER.info params[:_json][:params][:payment_term] , with [:params] being the top level attribute of the JSON object.

The output of RAILS_DEFAULT_LOGGER.info params[:_json][:params][:payment_term] looks like a url encoded string rather than a JSON object but if I try using JSON.parse(params[:_json]) I just get an an error which is TypeError (can't convert String into Integer) .

Any ideas? I know it must be some combination of what I have above and I'm sure I've tried them all but I must not have or I'm trying something completely irrelevant.

Full Controller

def save_draft
  if request.post?
    RAILS_DEFAULT_LOGGER.info "\n\n"
    RAILS_DEFAULT_LOGGER.info params
    RAILS_DEFAULT_LOGGER.info params[:_json]
    RAILS_DEFAULT_LOGGER.info params[:_json][:form_data]
    RAILS_DEFAULT_LOGGER.info params[:_json][:form_data][:payment_term]
    RAILS_DEFAULT_LOGGER.info "\n\n"

    render :text => '1'
  else
    render :text => '0'
  end
end

The current error from that is to do with the RAILS_DEFAULT_LOGGER.info params[:_json][:form_data] line, it's a no method error because of a nil object.

This is the output of RAILS_DEFAULT_LOGGER.info params.inspect

{"action"=>"save_draft", "_json"=>"form_data=%7B%22line_items%22%3A%5B%7B%22contract_term%22%3A12%2C%22product%22%3A%222-Pair+EFM+off-net%22%2C%22site%22%3Anull%2C%22quantity%22%3A1%2C%22setup_cost_each%22%3A1500%2C%22setup_cost%22%3A1500%2C%22rental_cost_each%22%3A240%2C%22rental_cost%22%3A240%2C%22is_ad_hoc%22%3Afalse%2C%22default_setup%22%3A1500%2C%22default_rental%22%3A240%2C%22default_contract%22%3A12%2C%22fixed_initial_cost%22%3Afalse%2C%22price_list_item_id%22%3A194%2C%22info%22%3A%22Provides+up+to+10Mb%2Fs+of+symmetric+Ethernet+connectivity+with+a+6-hour+fix+SLA.%22%7D%5D%2C%22partner_id%22%3A125%2C%22raised_by%22%3A940%2C%22approval_required%22%3A0%2C%22payment_term%22%3A%22Monthly%22%2C%22message%22%3A%22%22%2C%22scope_of_work%22%3A%22%22%2C%22internal_message%22%3A%22%22%2C%22partner_ref%22%3A%22%22%2C%22ccs%22%3A%22%22%2C%22project_requires_coordination%22%3A0%7D", "controller"=>"quotes"}

try this

$.ajax({
    url: '/quotes/save_draft',
    data: quote,
    type: 'POST',
    cache: false,
    success: function(data) {
      #your call 
    },
    error: function() {
      #show error
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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