dongtuo1482 2017-11-18 07:33
浏览 136

如何在控制器中发送Ajax数据

I want to recommend the visitors the hotels near by him based on his current location. My problem is how to get not login user latitude and longitude so that i can recommend him nearby hotel.

home.blade.php

    <script>
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            console.log(position);
            var lat = position.coords.latitude;
            var lng = position.coords.longitude;

            $.ajax({
                url:'http://localhost:8000/getgeo',
                type:'get',
                data:{latitude:lat,longitude:lng},

//even when  i set some value here data:{latitude:1222323}, i did not get `1222323` in controller


                success:function(data)
                {
                    alert('success');
                }

            });
        });
    }

    </script>

I got null in controller controller:

public function geo()
{
    return view('home');

}
public function getCoordinate(Request $request)
{
    return $request->latitude;
}

Here i am getting latitude and longitude at console in home.blade.php page .But now got in controller

Route:

Route::get('/geo', 'ProductController@geo');

Route::get('/getgeo', 'ProductController@getCoordinate');
  • 写回答

1条回答 默认 最新

  • dpzlz08480 2017-11-18 07:43
    关注

    Change following points:-

    change ajax url : url:'/getgeo'
    And function :-
    public function getCoordinate(Request $request)
     {
      if($request->ajax()){
        $data = $request->all();
        echo "<pre>"; print_r($data); // print all data here 
      }
    
     }  
     Try to change Route:-
     Route::any('/getgeo', 'ProductController@getCoordinate');
    
    评论

报告相同问题?

悬赏问题

  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真