Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send json request to rails using jquery

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?

like image 616
katsuya Avatar asked Apr 19 '11 19:04

katsuya


1 Answers

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:

$.post(url, { email: emailVal, password: passwordVal }, callback, "json").error(errorHandler);

It now works perfectly fine.

like image 147
katsuya Avatar answered Oct 12 '22 05:10

katsuya