簡體   English   中英

我如何將 ajax 成功函數返回給它被調用的方法

[英]How do i return an ajax success function to the method it was called

我想將 ajax 返回值返回到我調用它的函數中,但沒有返回任何數據。

function payment {
    var x = payment_status(status)
}

function payment_status (status) 
{
    return  $.ajax({
          url: "http://127.0.0.1:81/public/api/payment_status",
          type: 'POST',
          'content-type':'application/X-WWW-form-urlencoded',
          'Accept': 'application/json',
           data: {

                'status' : status,
                 }, success: function(response){}
           });
 }

$.ajax() 返回 thenable 或 Promise(在 jquery 3+ 中)參見https://www.promisejs.org/MDN 上的 Promise (或者這個答案

所以你可以使用async 函數(但也可以用 babel 轉譯它):

async function payment() {
  try {
    var response = await payment_status(status);
  } catch (error) {
    // Handle error
  }
}

function payment_status(status) {
  return $.ajax({
    url: "http://127.0.0.1:81/public/api/payment_status",
    type: "POST",
    "content-type": "application/X-WWW-form-urlencoded",
    Accept: "application/json",
    data: {
      status: status
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM