【问题标题】:ajax post geolocation not wokingajax发布地理位置不起作用
【发布时间】:2016-08-25 05:53:59
【问题描述】:

我想使用 AJAX 将数据从视图发送到控制器,但我的代码无法正常工作。我的代码有什么问题?当我单击按钮并打开时...我只是得到一个白屏。

我试过这个:

<button onclick="postLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
} else {
    x.innerHTML = "Geolocation is not supported by this browser.";
}

function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}

function postLocation() {
 var lat = [ + position.coords.latitude +];
var long =[ + position.coords.longitude];

 $.ajax({
      url: 'dosen/lokasi',
      type: "post",
      data: {lat:lat, long:long},
       success: function(response){ // What to do if we succeed
      if(data == "success")
    alert(response); 
  },
 error: function(response){
alert('Error'+response);
}
    });
}
}
</script>

控制器方法:

 $latuser = Input::File('lat');
 $longuser = Input::File('long');

   echo $latuser;

和路线:

 Route::get('/dosen/lokasi', 'DosenController@location');
 Route::post('/dosen/lokasi', 'DosenController@location');

【问题讨论】:

  • if(data == "success") // 这个数据变量来自哪里?

标签: javascript jquery ajax geolocation laravel-5.2


【解决方案1】:

检查一下:

$.ajax({
    url : 'your_url',
    method: 'post',    // it is get or post
    data:{
        var1 : val1,    // variables
        var2 : val2
    },
    success: function(response){    // variable to hold the server response
        if(response == 1)           // condition to check if ajax request task is done or not
        {
            // if part
        }
        else
        {
            // else part
        }
    }
});

现在相应地尝试一下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-31
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 2015-07-01
    相关资源
    最近更新 更多