Ruby on rails 对RubyonRails服务器的POST请求失败
我正在为移动应用程序构建一个带有一些API的rails应用程序 问题是所有POST请求都不起作用 以下是我如何称呼他们(从phonegap应用程序): 以下是服务器日志:Ruby on rails 对RubyonRails服务器的POST请求失败,ruby-on-rails,api,cordova,post,Ruby On Rails,Api,Cordova,Post,我正在为移动应用程序构建一个带有一些API的rails应用程序 问题是所有POST请求都不起作用 以下是我如何称呼他们(从phonegap应用程序): 以下是服务器日志: 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\"",
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
我解决了将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 });
不问题是您应用的语法。。写它
数据:{email:“我的email”,…}
直接转到..的示例。。查看JSON
datatypes的语法..我尝试不使用“”,但没有成功。奇怪的是,如果我通过chrome的postman插件发送请求,我会收到正确的数据!答案应该是答案,而不是问题的一部分。答案已编辑,谢谢。
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 });