Skip to Content
Technical Articles
Author's profile photo Stefan Schnell

How to use RFC_READ_TABLE from JavaScript via WebService

Here a step by step guide to expose the RFC-enabled function module RFC_READ_TABLE as WebService and how to use it via JavaScript:

  1. Start transaction code SE80 and create a new Enterprise Service./wp-content/uploads/2016/01/000_869349.jpg
  2. Choose Service Provider and press Continue button in the dialog.

    001.JPG
  3. Choose Existing ABAP Object and press Continue button.002.JPG
  4. Name the service definition, the description and press Continue button.003.JPG
  5. Choose Function Module and press Continue button.004.JPG
  6. Name Function Module with RFC_READ_TABLE.005.JPG
  7. Accept SOAP Appl, set Profile to Authentication with User and Password, No Transport Guarantee and press Continue button./wp-content/uploads/2016/01/006_869337.jpg
  8. Choose the Package you want, I my case I activate Local Object CheckBox, and press Continue button.007.JPG
  9. Press Complete button.008.JPG
  10. Now Save and Activate the Service Definition.009.JPG
  11. Start the transaction code SOAMANAGER, log on and select the link Web Service Configuration./wp-content/uploads/2016/01/0000_869351.jpg
  12. Search for your service name, in my case ZTEST. Press the Apply Selection button.010.JPG
  13. Choose Configurations tab below and choose the link Create.00000.JPG

    Set the Service Name, the Description, the Binding Name and press the button Apply Settings.

    011.JPG

  14. Scroll down and choose in Provider Security User ID/Password and Save your configuration.012.JPG
    013.JPG
  15. Choose the Overview tab and select the link Open WSDL document for selected binding or service./wp-content/uploads/2016/01/017_869353.jpg

    A new browser window will be opened, scroll down until address location tag. Here you find the URL of the web service.

    014.JPG

    Thats all to expose a RFC function module as WebService.

  16. Here now a complete request to call the WebService with the table USR01.POST /sap/bc/srt/rfc/sap/ztest2/001/ztest2/rfc_read_table HTTP/1.1
    Host: ABAP
    Accept: */*
    Authorization: Basic YmN1c2VyOm1pbmlzYXA=
    content-length: 428
    content-type: text/xml

    <?xml version=”1.0″ encoding=”UTF-8″?>
    <env:Envelope xmlns:env=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xs=”http://www.w3.org/2001/XMLSchema“>
    <env:Header/>
    <env:Body>
    <ns1:RFC_READ_TABLE xmlns:ns1=”urn:sap-com:document:sap:rfc:functions”>
    <DATA />
    <FIELDS />
    <OPTIONS />
    <QUERY_TABLE>USR01</QUERY_TABLE>
    </ns1:RFC_READ_TABLE>
    </env:Body>
    </env:Envelope>

    015.JPG

  17. Here now a JavaScript program to call the SAP WebService.
    <!doctype html>
    <html>
    
      <head>
        <title>Calling Web Service from jQuery</title>
    
        <script type="text/javascript" src="jquery.min.js"></script>
    
        <script type="text/javascript">
          $(document).ready(function () {
            $("#btnCallWebService").click(function (event) {
              var wsUrl = "http://ABAP:8080/sap/bc/srt/rfc/sap/ztest/001/ztest/rfc_read_table";
              var soapRequest = '<?xml version="1.0" encoding="UTF-8"?>' +
                '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">' +
    
                '<env:Body>' +
                '<ns1:RFC_READ_TABLE xmlns:ns1="urn:sap-com:document:sap:rfc:functions">' +
                '<DATA />' +
                '<FIELDS />' +
                '<OPTIONS />' +
                '<QUERY_TABLE>USR01</QUERY_TABLE>' +
                '</ns1:RFC_READ_TABLE>' +
                '</env:Body>' +
                '</env:Envelope>';
              $.ajax({
                type: "POST",
                url: wsUrl,
                contentType: "text/xml",
                dataType: "xml",
                headers: {
                  "Authorization": "Basic " + btoa("bcuser:minisap"),
                  "Accept": "*/*",
                  "Host": "ABAP"
                },
                data: soapRequest,
                success: processSuccess,
              });
                error: processError
            });
          });
    
          function processSuccess(data, status, req) { 
            alert('success');
            if (status == "success")
              alert(req.responseText);
          }
    
          function processError(data, status, req) {
            alert(req.responseText + " " + status);
          }
    
        </script>
    
      </head>
    
      <body>
        <h3>
            Calling SAP Web Services via JavaScript with jQuery/AJAX
        </h3>
        <input id="btnCallWebService" value="Call web service" type="button" />
      </body>
    
    </html>

    /wp-content/uploads/2016/01/016_869348.jpg

With this knowledge it should be now not difficult to expose any RFC-enable function module as WebService and to use it inside your JavaScript program.

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Stefan Schnell
      Stefan Schnell
      Blog Post Author

      Hello community,

      I test this szenario in a production environment, but I have some Cross-Origin Resource Sharing (CORS) trouble. My browser shows in the console: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 401. So I load the html file as bsp to the server and all works well and expcted.

      Here the advanced html code:

      <!doctype html>
      
      <html>
      
        <head>
      
          <title>Calling Web Service from jQuery</title>
      
          <script type="text/javascript">
          var Path = "/SAP/BC/BSP/SAP/PUBLIC/libraries/javascript";
          document.write("<script type='text/javascript' src='" + Path + 
            "/jquery.min.js'><\/script>");
          </script>
      
          <script type="text/javascript">
      
            $(document).ready(function () {
              $("#btnCallWebService").click(function (event) {
      
                var wsUrl = "http://ABAP:8000" + 
                  "/sap/bc/srt/rfc/sap/ztest/001/ztest/rfcreadtable";
      
                var soapRequest = '<?xml version="1.0" encoding="UTF-8"?>' +
                  '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" ' +
                  'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
                  'xmlns:xs="http://www.w3.org/2001/XMLSchema">' +
                  '<env:Body>' +
                  '<ns1:RFC_READ_TABLE xmlns:ns1="urn:sap-com:document:sap:rfc:functions">' +
                  '<DATA />' +
                  '<FIELDS>' +
                  '<item><FIELDNAME>BNAME</FIELDNAME></item>' +
                  '<item><FIELDNAME>SPLD</FIELDNAME></item>' +
                  '<item><FIELDNAME>LANGU</FIELDNAME></item>' +
                  '</FIELDS>' +
                  '<OPTIONS>' +
                  '<item><TEXT>BNAME = 'BCUSER'</TEXT></item>' +
                  '</OPTIONS>' +
                  '<DELIMITER>~</DELIMITER>' +
                  '<QUERY_TABLE>USR01</QUERY_TABLE>' +
                  '</ns1:RFC_READ_TABLE>' +
                  '</env:Body>' +
                  '</env:Envelope>';
      
                $.ajax({
                  type: "POST",
                  url: wsUrl,
                  contentType: "text/xml",
                  dataType: "xml",
                  headers: {
                    "Authorization": "Basic " + btoa("bcuser:secret"),
                    "Accept": "*/*"
                  },
                  data: soapRequest,
                  success: processSuccess,
                });
                  error: processError
              });
            });
      
            function processSuccess(data, status, req) {
              alert('success');
              if (status == "success")
                alert(req.responseText);
            }
      
            function processError(data, status, req) {
              alert(req.responseText + " " + status);
            }
      
          </script>
      
        </head>
      
        <body>
      
          <h3>
              Calling SAP Web Services via JavaScript with jQuery/AJAX
          </h3>
      
          <input id="btnCallWebService" value="Call web service" type="button" />
      
        </body>
      
      </html>

      Enjoy it.

      Cheers

      Stefan

      Author's profile photo Pras Venkat
      Pras Venkat

      Hi Stefan,

      Have been searching through on webservices in ABAP. Its a wonderful article. Thanks !