开发者

rails and jquery post

开发者 https://www.devze.com 2023-03-07 17:59 出处:网络
im using this jquery post navigator.geolocation.getCurrentPosition(function(position){ $.post(\'/marketplace\',{latitude: position.coords.latitude, longitude: position.coords.longitude})

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 :(


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.


This will create xhr request.for that you have to create

@lat_long={:lat=>request.params[:latitude],:lng=>request.params[:longitude]}
render :text=> @lat_long and return false

use above line in your controller and in jquery

$.post('/marketplace',{latitude: position.coords.latitude, longitude: position.coords.longitude},function(data) {
   var lat=data.lat;
   var lng=data.lng
 });
0

精彩评论

暂无评论...
验证码 换一张
取 消