0

我正在尝试向我的 Rails 3 应用程序发出一个简单的 PUT JSON 请求:

$.ajax({type: 'PUT', url: '/controlpanel/custom_forms/1', data: { hello: 'world' }, contentType: 'json'})

不知何故,数据没有收到,params只包含id。这是在服务器端:

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"}

我期待Parameters成为{"id"=>"1", "hello"=>"world"}

该请求在 Chrome 中看起来不错:

请求标头

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

请求有效载荷

hello=world

我究竟做错了什么?

4

1 回答 1

1

我犯了两个错误。首先,contentType必须设置为application/json,而不仅仅是json。其次,data必须手动序列化JSON.stringify({...}).

于 2012-05-23T03:26:08.640 回答