If I want to separate out my ajax success
function so that it is defined elsewhere in my <script>
, does it have to be inside the
$(document).ready(function()
{
section or could it be defined along with non-jQuery javascript functions?
$.ajax(
{
url: '/load_prayer',
cache: false,
dataType: 'json',
type: 'POST',
data: ({ 'prayerId' : prayerId }),
success: function(data)
{
$('#prayer_date').html(data.Date);
console.log(data);
},
error: function(e, xhr)
{
console.log(e);
}
});
The reason I don't want to define it inside the call to ajax
is it will eventually be a large function and it will be confusing to read if it's mixed in with the other ajax
call parameters.
For example, would this work:
$.ajax(
{
url: '/load_prayer',
cache: false,
dataType: 'json',
type: 'POST',
data: ({ 'prayerId' : prayerId }),
success: handlePrayer(data),
error: function(e, xhr)
{
console.log(e);
}
});
handlePrayer(data)
{
$('#prayer_date').html(data.Date);
console.log(data);
}