Quantcast
Channel: SCN : Blog List - All Communities
Viewing all articles
Browse latest Browse all 2548

How to use RFC_READ_TABLE from JavaScript via WebService

$
0
0

Hello community,

 

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.
    000.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.
    006.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.

    0000.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.
    017.jpg
    A new browser window is 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 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>

    016.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.

 

Enjoy it.

 

Cheers

Stefan


Viewing all articles
Browse latest Browse all 2548

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>