Skip to content
Advertisement

how to call function inside ajax call

I am trying to call function within ajax success block which is not happening.

Below I have given code which i was tried.

$("#form-data").submit(function(e) {
  e.preventDefault();
  var me = this;
  $.ajax({
    type: "POST",
    url: "{{route('storeData.store')}}",
    data: fd,
    processData: false,
    contentType: false,
    success: function(data) {
      me.callFunc(); // here i need to call that fucntion once data is stored in database
    }
  });
})

$(document).ready(function(e) { // here i need to call that fucntion when page loads
  this.callFunc();
})

function callFunc() { // this is the function needs to call 
  $.ajax({
    type: "GET",
    url: "{{route('getData.get')}}",
    success: function(data) {
      console.log("output data", data)
    }
  })
}

Advertisement

Answer

Call your “callFunc()” without me / this as per below.

$("#form-data").submit(function(e) {
  e.preventDefault();
  $.ajax({
    type: "POST",
    url: "{{route('storeData.store')}}",
    data: fd,
    processData: false,
    contentType: false,
    success: function(data) {
      callFunc(); // here i need to call that fucntion once data is stored in database
    }
  });
})

$(document).ready(function(e) { // here i need to call that fucntion when page loads
  callFunc();
})

function callFunc() { // this is the function needs to call 
  $.ajax({
    type: "GET",
    url: "{{route('getData.get')}}",
    success: function(data) {
      console.log("output data", data)
    }
  })
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement