简体   繁体   中英

ajax post geolocation not woking

I want to send data from view to controller using AJAX but my code is not working. What is wrong with my code ? when I click the button and open ...I just got a white screen.

I tried this:

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

controller method :

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

   echo $latuser;

and route :

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

Check this:

$.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
        }
    }
});

Now try it accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM