我想使用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');
答案 0 :(得分: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
}
}
});
现在试试吧。