我正在构建一个带有移动应用程序api的rails应用程序。
问题是所有POST请求都不起作用。
以下是我如何称呼他们(来自手机应用程序):
72 $.ajax({
73 url: 'http://www.my-website.com:port/api/login',
74 type: 'POST',
75 dataType: 'json',
76 data:{"email":"my-email",
77 "password":"my-password"},
78 success: function(data){
79 alert(data);
80 },
81 error: function (event, jqXHR, ajaxSettings, thrownError) {
82 alert('[event:' + event + '], [jqXHR:' + jqXHR + '], [ajaxSettings:' + ajaxSettings + '], [thrownError:' + thrownError + '])');
83 }
84 });
这是服务器日志:
Started POST "/api/login" for xxx.xxx.xxx.xxx at 2015-04-03 10:58:51 +0000
Processing by ApiController#login as HTML
Parameters: {"email"=>"\"my-email.com\"", "password"=>"[FILTERED]"}
Completed 500 Internal Server Error in 2ms
JSON::ParserError (A JSON text must at least contain two octets!):
app/controllers/api_controller.rb:71:in `parse_request'
这里我在api控制器中收到错误:
before_filter :parse_request
...
def parse_request
@json = JSON.parse(request.body.read)
end
答案 0 :(得分:0)
我解决了将ajax请求代码更改为此问题的问题:
69 var dt={
70 "email":"my-email",
71 "password":"my-password"
72 }
73
74 $('#submit').click(function(){
75 //localStorage['id'] = 10;
76 //if (localStorage != null)
77 // window.location='index-logged.html';
78 $.ajax({
79 url: 'http://www.my-website.com/api/login',
80 type: 'POST',
81 data: JSON.stringify(dt),
82 cache: false,
83 success: function(data){
84 alert(data);
85 },
86 error: function (event, jqXHR, ajaxSettings, thrownError) {
87 alert('[event:' + event + '], [jqXHR:' + jqXHR + '], [ajaxSettings:' + ajaxSettings + '], [thrownError:' + thrownError + '])'
88 }
89 });
90 });