2016-08-25 41 views
0

我想從視圖發送數據到控制器使用AJAX,但我的代碼不起作用。我的代碼有什麼問題?當我點擊按鈕並打開...我剛剛看到一個白色的屏幕。ajax post geolocation not woking

我嘗試這樣做:

<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', '[email protected]'); 
Route::post('/dosen/lokasi', '[email protected]'); 
+0

if(data ==「success」)//這個數據變量來自哪裏? –

回答

0

檢查:

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

現在嘗試相應。

+0

仍然無法正常工作 – Ezra