Rails 和 jquery 帖子
我使用这个 jquery post
navigator.geolocation.getCurrentPosition(function(position){
$.post('/marketplace',{latitude: position.coords.latitude, longitude: position.coords.longitude})
});
路由:
post '/marketplace' => 'pages#marketplace'
在控制器中 Rails.logger.debug('Jquery.POST' + params.inspect)
在控制台中打印以下信息
Started POST "/marketplace" for 127.0.0.1 at 2011-05-20 13:23:16 +0300
Processing by PagesController#marketplace as
Parameters: {"latitude"=>"44.508851", "longitude"=>"33.600509"}
Jquery.POST{"latitude"=>"44.508851", "longitude"=>"33.600509", "controller"=>"pages", "action"=>"marketplace"}
另外,firebug 表示纬度 44.508851 和经度 33.600509 已成功发送。 但在视图中我没有我的帖子数据,= params.inspect
{"action"=>"marketplace", "controller"=>"pages"}
如何在控制器中访问我的 jquery 帖子数据?
*更新
@lat = request.params[:latitude]
@long = request.params[:longitude]
视图中的两个 nil :(
im using this jquery post
navigator.geolocation.getCurrentPosition(function(position){
$.post('/marketplace',{latitude: position.coords.latitude, longitude: position.coords.longitude})
});
routes:
post '/marketplace' => 'pages#marketplace'
in controller Rails.logger.debug('Jquery.POST' + params.inspect)
print in the console following
Started POST "/marketplace" for 127.0.0.1 at 2011-05-20 13:23:16 +0300
Processing by PagesController#marketplace as
Parameters: {"latitude"=>"44.508851", "longitude"=>"33.600509"}
Jquery.POST{"latitude"=>"44.508851", "longitude"=>"33.600509", "controller"=>"pages", "action"=>"marketplace"}
Also firebug says that latitude 44.508851 and longitude 33.600509 successfully sent.
But in the view i dont have my post data, = params.inspect
{"action"=>"marketplace", "controller"=>"pages"}
How to access my jquery post data in the controller?
*Update
@lat = request.params[:latitude]
@long = request.params[:longitude]
both nil in the view :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是 csrf 令牌的问题吗?您确定它会与您的其他 POST 数据一起发送吗?
如果您使用 jquery-rails 插件,请务必获取最新版本,因为处理方式发生了一些变化。如果您在文档的
head
中获得了令牌,则该插件应自动将其注入到每个 xhr POST 请求中。请查看 github 页面了解更多信息。Could this be an issue with the csrf token? Are you sure it's being sent along with the rest of your POST data?
If you're using the jquery-rails plugin, be sure to grab the latest version since there's been some changes with the way this is being handled. The plugin should automatically inject it into each xhr POST request if you've got the token in the
head
of your document. Check out the github page for more info.这将创建 xhr 请求。为此,您必须
在控制器和 jquery 中创建使用上面的行
This will create xhr request.for that you have to create
use above line in your controller and in jquery