簡體   English   中英

如何在 ajax 內調用 function

[英]how to call function inside ajax call

我試圖在 ajax 成功塊內調用 function ,但沒有發生。

下面我給出了我嘗試過的代碼。

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

在沒有我/這個的情況下調用你的“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)
    }
  })
}

您的 function 在全球范圍內可用,因此只需像其他任何 javascript function 一樣在任何其他 ZC1C425Z0468E68384F144 中調用它

callFunc()

你不必使用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