简体   繁体   English

如何在 ajax 内调用 function

[英]how to call function inside ajax call

I am trying to call function within ajax success block which is not happening.我试图在 ajax 成功块内调用 function ,但没有发生。

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)
    }
  })
}

Call your "callFunc()" without me / this as per below.在没有我/这个的情况下调用你的“callFunc()”,如下所示。

$("#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)
    }
  })
}

Your function is globally available so just call it like any other javascript function inside any other function您的 function 在全球范围内可用,因此只需像其他任何 javascript function 一样在任何其他 ZC1C425Z0468E68384F144 中调用它

callFunc()

You don't have to use this你不必使用this

 $(document).ready(function (e) { function callFunc() { // this is the function needs to call $.ajax({ type: "GET", url: "{{route('getData.get')}}", success: function (data) { console.log("output data", data) } }) } $("#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 } }); }); callFunc(); // here i need to call that fucntion when page loads });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM