POST Request With Response using AJAX

jQuery provides several methods for AJAX functionality.

With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post – And you can load the external data directly into the selected HTML elements of your web page!

The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where other methods cannot be used.

function paymentProcessRequest(cardDetails, transactionId, customerId, recordType) {
      return new Promise(function (resolve) {
            $.ajax({
                type: "POST",
                url: "https://tstdrv2747005.netsuite.com/app/site/scriptlet.nl?script=91&deploy=1",
               dataType: "json",
               data: {
                  cardDetails: JSON.stringify(cardDetails),
                  transactionId: transactionId,
                  customerId: customerId,
                  recordType: recordType,
                },
                success: function (response) {
                    resolve(response);
                }
           });
     });
}

paymentProcessRequest(cardDetails, transactionId, customerId, recordType).then(function (response) {
      //  callback, handle the response
      if (response.data === 'Approval') {
             // Payment Success
      } else {
             // Payment failure
      }
});


The code snippet illustrates a payment processing function that utilizes JavaScript promises and AJAX to send a request to a server-side suitelet.

  • The `paymentProcessRequest` function is defined, which takes several parameters representing card details, transaction ID, customer ID, record type, and a new profile indicator. It returns a Promise object to handle the asynchronous nature of the AJAX request.
  • Within the `paymentProcessRequest` function, an AJAX request is configured using the `$.ajax` method provided by jQuery. The request is set to use the POST method and targets a specific URL endpoint, where the server-side scriptlet resides.
  • The AJAX request includes various data parameters, such as `cardDetails`, `transactionId`, `customerId` and a `recordType` value.
  • In the AJAX configuration, a success callback function is defined. When the server responds successfully, the function is executed. Inside the success callback, the `response` is passed to the `resolve` method of the Promise, allowing access to the response data in the subsequent `then` block.
  • After the `paymentProcessRequest` function is called, a `then` block follows, where the response can be handled based on its content. In this example, if the `response.data` is equal to `’Approval’`, it signifies a successful payment. Otherwise, it indicates a payment failure.

Leave a comment

Your email address will not be published. Required fields are marked *