简体   繁体   中英

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

I want to return the ajax returned value in back to my function where it was called but no data was returned.

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() returns a thenable or a Promise (in jquery 3+) See https://www.promisejs.org/ or Promise on MDN (or maybe this answer )

So you can use an async function (but maybe transpile it with 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
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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