data : JSON.stringify(location),
jQuery Pass Data Parameter to Function
Hi all,
I'm trying to pass the 'data' parameter of my AJAX call which is within a function and it's not working. My Javascript/jQuery is below:
$(document).ready(function(){
function geocode(location){
$.ajax({
type: 'post',
url: '/test',
data: location,
dataType: 'json',
success: function(data){
//alert(data);
$('#exampleModal .modal-body').html(data);
$('#exampleModal').modal('toggle');
}
});
}
// BOF Current location...
$('.current-location').click(function(){
if ("geolocation" in navigator){ //check geolocation available
//try to get user current location using getCurrentPosition() method
navigator.geolocation.getCurrentPosition(function(position){
//Run AJAX...
location = { lat: position.coords.latitude, lng: position.coords.longitude };
geocode(location);
});
}else{
alert('Your browser does not support geolocation!');
}
});
// EOF Current location...
});
Upon submit, I'm just redirected to the following: http://localhost:8888/[object%20Object]
To be clear, I'm trying to pass JSON to the function as a parameter but it's not having it. Any help would be hugely appreciated. You will save my bacon!
This example is not your code:
<script>
$(function () {
function makeAJAXRequest (text) {
$.ajax({
url: 'owner/getowner',
type: 'GET',
dataType: 'json',
data: { id: text },
success: processSuccess
});
}
function processSuccess (data) {
$.each(data, function (key, value) {
var id = '#' + key,
elem = window.opener.$(id);
if (key === 'ocheck') {
elem.attr('checked', value === '1');
}
else {
elem.val(value);
}
});
self.close();
}
$("#myTable td:nth-child(1)").click(function (event) {
event.preventDefault();
var $td = $(this).closest('tr').children('td');
var currentCellText = $td.eq(0).text();
var CellText = $td.eq(1).text();
makeAJAXRequest(currentCellText);
});
});
</script>
Take note of
makeAJAXRequest(currentCellText);
So basically, play around with having some functions you can re-use, such as makeAJAXRequest.
Backup all first and just see what you come up with.
Your
$.ajax({
type: 'post',
url: '/test',
data: { lat: position.coords.latitude, lng: position.coords.longitude },
success: function(data){
is completely re-usable by sending it variables instead of hard coded data, i.e., /test could be sent as a variable for use.
I hope you get the general idea.
Please or to participate in this conversation.