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: "example@test.com", password: "password"},
success: onSuccess,
error: onError,
dataType: "json"
});
我想把JSON post请求发送到rails 3服务器。我有以下ajax请求:$。ajax({类型:'POST',内容类型:"application/json", url: url,数据:{email: "example@test.com",密码:"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"=>"exmaple@test.com", "password"=>"[FILTERED]"}
但是,rails服务器接收数据如下:{"_json"=>["object "]},我希望它接收为:{"email"=>"exmaple@test.com", "password"=>"[滤波]"}
I think this is happening because the jquery wraps the data with _json object if the content type is json.
我认为这是因为jquery使用_json对象包装数据,如果内容类型是json。
Does anybody know how I should do this?
有人知道我该怎么做吗?
3 个解决方案
#1
4
This 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:
这是由于旧版本jquery中的错误造成的。我现在使用jquery 1.5版本,并发送如下帖子请求:
$.post(url, { email: emailVal, password: passwordVal }, callback, "json").error(errorHandler);
It now works perfectly fine.
它现在运行得非常好。
#2
2
have you tried doing the serialization yourself (using jQuery.param)?
您是否尝试过自己进行序列化(使用jQuery.param)?
jQuery.param({email: "example@test.com", password: "password"})
==> "email=example%40test.com&password=password"
So that your ajax request becomes:
这样,您的ajax请求变成:
$.ajax({ type: 'POST',
contentType: "application/json",
url: url, data: $.param({email: "example@test.com", password: "password"}),
success: onSuccess,
error: onError,
dataType: "json"
});
#3
0
According to jquery docs it seems like if you pass in an object to data it will try some automatic deserialization.
根据jquery文档,似乎如果你将一个对象传递给数据,它会尝试一些自动反序列化。
Set processData: false
and then set data to json string.
设置processData: false,然后将数据设置为json字符串。
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/jQuery.ajax/
#1
4
This 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:
这是由于旧版本jquery中的错误造成的。我现在使用jquery 1.5版本,并发送如下帖子请求:
$.post(url, { email: emailVal, password: passwordVal }, callback, "json").error(errorHandler);
It now works perfectly fine.
它现在运行得非常好。
#2
2
have you tried doing the serialization yourself (using jQuery.param)?
您是否尝试过自己进行序列化(使用jQuery.param)?
jQuery.param({email: "example@test.com", password: "password"})
==> "email=example%40test.com&password=password"
So that your ajax request becomes:
这样,您的ajax请求变成:
$.ajax({ type: 'POST',
contentType: "application/json",
url: url, data: $.param({email: "example@test.com", password: "password"}),
success: onSuccess,
error: onError,
dataType: "json"
});
#3
0
According to jquery docs it seems like if you pass in an object to data it will try some automatic deserialization.
根据jquery文档,似乎如果你将一个对象传递给数据,它会尝试一些自动反序列化。
Set processData: false
and then set data to json string.
设置processData: false,然后将数据设置为json字符串。
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/jQuery.ajax/