Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8288459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:17:18+00:00 2026-06-08T12:17:18+00:00

Every time I want to POST an array of JSON objects with jQuery to

  • 0

Every time I want to POST an array of JSON objects with jQuery to Rails, I have this problem.
If I stringify the array I can see that jQuery is doing its work correctly:

"shared_items"=>"[{\"entity_id\":\"253\",\"position\":1},{\"entity_id\":\"823\",\"position\":2}]"

But if I just send the array it as the data of the AJAX call I get:

"shared_items"=>{"0"=>{"entity_id"=>"253", "position"=>"1"}, "1"=>{"entity_id"=>"823", "position"=>"2"}}

Whereas if I just send a plain array it works:

"shared_items"=>["entity_253"]

Why is Rails changing the array to that strange hash? The only reason that comes to mind is that Rails can’t correctly understand the contents because there is no type here (is there a way to set it in the jQuery call?):

Processing by SharedListsController#create as 

Thank you!

Update:
I’m sending the data as an array, not a string and the array is created dynamically using the .push() function. Tried with $.post and $.ajax, same result.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:17:19+00:00Added an answer on June 8, 2026 at 12:17 pm

    In case someone stumbles upon this and wants a better solution, you can specify the “contentType: ‘application/json'” option in the .ajax call and have Rails properly parse the JSON object without garbling it into integer-keyed hashes with all-string values.

    So, to summarize, my problem was that this:

    $.ajax({
      type : "POST",
      url :  'http://localhost:3001/plugin/bulk_import/',
      dataType: 'json',
      data : {"shared_items": [{"entity_id":"253","position":1}, {"entity_id":"823","position":2}]}
    });
    

    resulted in Rails parsing things as:

    Parameters: {"shared_items"=>{"0"=>{"entity_id"=>"253", "position"=>"1"}, "1"=>{"entity_id"=>"823", "position"=>"2"}}}
    

    whereas this (NOTE: we’re now stringifying the javascript object and specifying a content type, so rails will know how to parse our string):

    $.ajax({
      type : "POST",
      url :  'http://localhost:3001/plugin/bulk_import/',
      dataType: 'json',
      contentType: 'application/json',
      data : JSON.stringify({"shared_items": [{"entity_id":"253","position":1}, {"entity_id":"823","position":2}]})
    });
    

    results in a nice object in Rails:

    Parameters: {"shared_items"=>[{"entity_id"=>"253", "position"=>1}, {"entity_id"=>"823", "position"=>2}]}
    

    This works for me in Rails 3, on Ruby 1.9.3.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with my form,i have this error every time i want
In symfony 2 controllers, every time I want to get a value from post
It seems that every time I want to perform a db query, I have
I have a an array I want, whenenever i submit my form the post
It would be inconvenient if every time I want to post a news story
im fetching with the ajax() method in jQuery data and i want every time
I have a Ruby array which contains Post objects (a Mongoid model) with a
Everytime I pass post array data it overwrites itself every time a new selection
Seriously, it seems like every time I want to make my UI elements talk
In my activity there is a toggle button. I want every time you change

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.