# REST requests with jQuery

**URL:** <https://www.ruby-forum.com/t/rest-requests-with-jquery/178110>\
**Category:** Rails\
**Created:** [October 29, 2009, 10:47am UTC](https://www.ruby-forum.com/t/rest-requests-with-jquery/178110 "2009-10-29T10:47:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rainer\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@Rainer\_B](https://www.ruby-forum.com/u/Rainer_B)\
**Post date:** [October 29, 2009, 10:47am UTC](https://www.ruby-forum.com/t/rest-requests-with-jquery/178110/1 "2009-10-29T10:47:59Z")

</div>

Hi,

I’m trying to do a simple REST request to my rails app using jQuery  
but can’t get the data body right. It looks like parts are working and  
parts aren’t. I want to update a model so I have to use PUT, which I  
emulate by using a POST request and adding the usual “\_method” =\>  
“put”. This seems to work, but the actual model data does not come  
through. Here is the code:

var grpid = 138  
var grp\_data = {  
“\_method”: “put”,  
“grouping”: {  
“pos\_x”: col,  
“pos\_y”: row  
}  
}  
$.post(’/groupings/’+grpid+’.json’, grp\_data, null, ‘json’);

All I get in my rails app is:

Processing GroupingsController#update to json (for 127.0.0.1 at  
2009-10-28 12:05:33) [PUT]  
Parameters: {“grouping”=\>"[object Object]", “id”=\>“138”}  
Grouping Load (0.5ms) SELECT \* FROM “groupings” WHERE  
(“groupings”.“id” = 138)  
NoMethodError (undefined method `stringify_keys!' for "[object Object]":String): app/controllers/groupings_controller.rb:68:in`update’

So obviously the “\_method” is interpreted correctly (I’ll end up  
seeing a PUT) but the data is not. I found a solution for now, which  
is the following:

$.ajax({  
type: “PUT”,  
url:’/groupings/’+grpid+’.json’,  
data: JSON.stringify(grp\_data),  
contentType: ‘application/json; charset=utf-8’  
});

But this solution ignores the “\_method” information, so if I replace  
“PUT” with “POST”, again it doesn’t work (but as far as I understand  
“PUT” shouldn’t be used for compatibility reasons).

I’m pretty sure I’m missing something fundamental, since this  
operation should be a piece of cake and probably is being used all the  
time, so it can’t be that complicated. Especially the JSON.stringify()  
in my solution seems clumsy …

Anyone who sees what I’m doing wrong?

BTW: This application is running on rails 2.3.4

---

<div class="post-metadata">

**Author:** ![Rainer\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@Rainer\_B](https://www.ruby-forum.com/u/Rainer_B)\
**Post date:** [October 29, 2009, 1:02pm UTC](https://www.ruby-forum.com/t/rest-requests-with-jquery/178110/2 "2009-10-29T13:02:01Z")

</div>

Have you tried just:

$.ajax({  
type: “PUT”,  
url:‘/groupings/’+grpid+‘.json’,  
data: grp\_data,  
contentType: ‘application/json; charset=utf-8’  
});

Without the JSON.stringify call?

Greg

On Oct 29, 5:32 am, Rainer B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Rainer\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e274bd/32.png) [@Rainer\_B](https://www.ruby-forum.com/u/Rainer_B)\
**Post date:** [October 29, 2009, 4:01pm UTC](https://www.ruby-forum.com/t/rest-requests-with-jquery/178110/3 "2009-10-29T16:01:44Z")

</div>

Yes I have tried that, without the JSON.stringify() I don’t get an  
error but it doesn’t work either, I get the following result:

Processing GroupingsController#update to json (for 127.0.0.1 at  
2009-10-29 15:05:27) [PUT]  
Parameters: {“id”=\>“140”, “\_json”=\>“\_method=put&grouping=%5Bobject  
+Object%5D”}  
Grouping Load (0.5ms) SELECT \* FROM “groupings” WHERE  
(“groupings”.“id” = 140)  
Completed in 14ms (View: 1, DB: 1) | 200 OK [[http://localhost/](http://localhost/)  
groupings/140.json]
