Reputation: 79562
I'm trying to make a simple PUT JSON request to my Rails 3 app:
$.ajax({type: 'PUT', url: '/controlpanel/custom_forms/1', data: { hello: 'world' }, contentType: 'json'})
Somehow, the data is not received and params
only contains the id. Here's on the server side:
Started PUT "/controlpanel/custom_forms/1" for 127.0.0.1 at 2012-05-22 23:05:25 -0400
Processing by Controlpanel::CustomFormsController#update as */*
Parameters: {"id"=>"1"}
I'm expecting Parameters
to be {"id"=>"1", "hello"=>"world"}
The request looks fine in Chrome:
Request headers
PUT /controlpanel/custom_forms/1 HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Content-Length: 11
Origin: localhost:3000
X-CSRF-Token: HgGPJmgf0iXlsHtmZrifqFoww/rlzq+hAKb63HbAl8g=
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5
Content-Type: json
Accept: */*
Referer: http://localhost:3000/controlpanel/custom_forms/1/edit?feature_id=1
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: user_credentials=whatever
Request Payload
hello=world
What am I doing wrong?
Upvotes: 0
Views: 576
Reputation: 79562
I was making two mistakes. First, contentType
must be set to application/json
, not just json
. Secondly, the data
must be serialized manually with JSON.stringify({...})
.
Upvotes: 1