admin 管理员组文章数量: 1086019
I would like to send JSON post request to rails 3 server. I have following ajax request:
$.ajax({
type: 'POST',
contentType: "application/json",
url: url,
data: {email: "[email protected]", password: "password"},
success: onSuccess,
error: onError,
dataType: "json"
});
However the rails server receive the data as following:
{"_json"=>["object Object"]}
Where I want it to receive it as:
{"email"=>"[email protected]", "password"=>"[FILTERED]"}
I think this is happening because the jquery wraps the data with _json object if the content type is json.
Does anybody know how I should do this?
I would like to send JSON post request to rails 3 server. I have following ajax request:
$.ajax({
type: 'POST',
contentType: "application/json",
url: url,
data: {email: "[email protected]", password: "password"},
success: onSuccess,
error: onError,
dataType: "json"
});
However the rails server receive the data as following:
{"_json"=>["object Object"]}
Where I want it to receive it as:
{"email"=>"[email protected]", "password"=>"[FILTERED]"}
I think this is happening because the jquery wraps the data with _json object if the content type is json.
Does anybody know how I should do this?
Share Improve this question asked Apr 19, 2011 at 19:26 katsuyakatsuya 1,2043 gold badges16 silver badges21 bronze badges3 Answers
Reset to default 4This turns out to be because of bugs in old version of jquery. I now user jquery version 1.5 and send post request as follow:
$.post(url, { email: emailVal, password: passwordVal }, callback, "json").error(errorHandler);
It now works perfectly fine.
have you tried doing the serialization yourself (using jQuery.param)?
jQuery.param({email: "[email protected]", password: "password"})
==> "email=example%40test.&password=password"
So that your ajax request bees:
$.ajax({ type: 'POST',
contentType: "application/json",
url: url, data: $.param({email: "[email protected]", password: "password"}),
success: onSuccess,
error: onError,
dataType: "json"
});
According to jquery docs it seems like if you pass in an object to data it will try some automatic deserialization.
Set processData: false
and then set data to json string.
http://api.jquery./jQuery.ajax/
本文标签: javascriptHow to send json request to rails using jqueryStack Overflow
版权声明:本文标题:javascript - How to send json request to rails using jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1743984576a2513629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论