我有一个非常艰难的时间让json从javascript发布到支持rails的Web服务。
这是jquery代码;
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
var jsonPost = {
"email":"dummy@dummy.com",
"token":"63uO6eEfLVBFpnZswzI",
"content": "testcontent",
"notification_type": "2",
"name":"testname",
}
function callback(data){alert(data)};
jQuery.ajax({
type: "POST",
url: 'http://localhost:3000/api/create.json',
data: jsonPost,
success: callback,
dataType: "json",
contentType: "application/json",
processData: false
});
</script>
在铁轨方面:
def api_create
respond_to do |format|
format.json {
email = params[:email]
token = params[:token]
auto_action = params[:auto_action]
rails端确实将请求识别为JSON,但在rails中调试时,我看到请求中没有数据。
有人能指出我正确的方向吗?
答案 0 :(得分:2)
如果你在rails中做相反的事情,你不需要将你的JSON转换为字符串吗?
jQuery.ajax({
type: "POST",
url: 'http://localhost:3000/api/create.json',
data: JSON.stringify(jsonPost),
success: callback,
dataType: "json",
contentType: "application/json",
processData: false
});