0

I've the following request that works perfectly with curl, but I can't make it work with jQuery's jQuery.ajax family of functions:

curl -v -s -X GET -d 'matching[][name]=ottensen' http://localhost:3000/locations

The trick is the matching[][name]=ottensen. This produces, when interpreted by the application server the following:

params: { "matching" => [{"name" => "ottensen"}] }

In other words, I have passed an Array of hashes.

The curl syntax does seem strange, but also rather widely supported.

I've tried the following with jQuery (latest):

$.ajax({
    url: 'http://localhost:3000/locations',
    data: {
        "matching": [{
            "name": "ottensen"
        }]
    })
})   

And various other intricate combinations, unfortunately jQuery.serialize doesn't play nicely, it's docs do however mention:

Note: Because there is no universally agreed-upon specification for param strings, it is not possible to encode complex data structures using this method in a manner that works ideally across all languages supporting such input. Until such time that there is, the $.param method will remain in its current form.

There are some suggested JSON.stringify(obj) workarounds suggested, but the API has to remain accessible to the existing clients, and we can't change it to accept JSON, and we shouldn't have to, this supported by many Rack apps, and seems to be some kind of standard (perhaps only a de-facto standard), here's an example (which by and large appears to work as one might expect, but I can't change the backend server):

$.ajax({
    url: 'http://localhost:3000/locations',
    data: JSON.stringify({
        "matching": [{
            "name": "ottensen"
        }]
    }),
    contentType: 'application/json',
    dataType: 'json'
})

Question: Thus my question, how can I get jQuery to send the same thing to the server that curl is doing?

Side Note (Serverside Workaround): The server has this magical line of code, which makes me really, really sad: matching = params.fetch(:matching); matching = Array(matching.values) if matching.is_a?(Hash), it's a workaround that works for now, but I'd prefer not to have to duplicate that throughout my code.

It's also worth noting that I've considered implementing a Rack middleware to fix the passed jQuery style objects, so they work as expected with Rails (that is, re-write the params from a jQuery request that look like that, and fix them so the Rails application doesn't know anything has happened)

Lee Hambley
  • 6,270
  • 5
  • 49
  • 81
  • Also, not a duplicate of: http://stackoverflow.com/questions/6410810/rails-not-decoding-json-from-jquery-correctly-array-becoming-a-hash-with-intege as the answer isn't suitable. – Lee Hambley Oct 09 '12 at 23:36
  • I sort of understand what you are asking because I've done similar with PHP server-side. Would it be fair to rephrase your question as follows, "Question: How can I get jQuery to compose (and issue) a request that either emulates the curl expression or otherwise causes the curl expression to be executed?" – Beetroot-Beetroot Oct 10 '12 at 00:17
  • Have you looked at the actual request (headers and body) that is sent to the server? Can you paste it there? – F.X. Oct 10 '12 at 00:51

0 Answers0