Friday, September 7, 2018

Alternative to Using AJAX in SuiteScript

AJAX or Asynchronous Javascript and XML is a technique that allows developers to retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page. This uses XMLHttpRequest (XHR), a DOM API that can be used by JavaScript and other web browser scripting languages to transfer XML and other text data between a web server and a browser (as described in http://en.wikipedia.org/wiki/XMLHttpRequest). A good application is when there's a need to include third party CAPTCHA (see http://www.captcha.net/) capabilities in a Suitelet designed to be the replacement for an online form.

It is not possible to use the XMLHTTP object in Netsuite due to cross-domain scripting security constraints imposed by all browsers. What can be used instead is the nlapiRequestURL which works in both client and server SuiteScript and one of it's parameters  includes an optional callback function called when the request is completed (Client SuiteScript only). This provides equal functionality as offered by the AJAX approach. 

Here is a sample code accessing the External URL of another Suitelet:

function sendRequestNetsuiteWay()
{
       var resp = nlapiRequestURL( "http://external_url", null, null, handleResponseNetsuiteWay );
}

function handleResponseNetsuiteWay(response)
{
       if (response.getCode() == 200)
       {
              var xmlText = response.getBody();
              var xmlHTTPReq = nlapiStringToXML( xmlText );
              var msgNode = nlapiSelectNode( xmlHTTPReq, "//*[name()='mynode']" );
              var returnedVal  = nlapiSelectValue( msgNode, "//*[name()='mynodevalue']"  );
       }
       else
       {
              alert( "Staus is..."+ response.getCode());
       }
}

 

Note: nlapiRequestURL does not work work on online forms.



 

DISCLAIMER: The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Netsuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.

Netsuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Netsuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.

Netsuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code

 

No comments:

Post a Comment