Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to read response from local html file containing java script

Sucheta
Participant
0 Kudos

I need to read response from my local html file in my sap program. I have seen most of the sample code tries to read URL. If I try those codes it is checking the 'https:' or 'http' in the URL and as I have local file ( file:///C:/Users/COMPANY/Desktop/SIG/Data/test1.html) it is not getting http and showing error. Please help.

Contents of html file is as follows:

<html>

<head>

<SCRIPT type="text/javascript">

function testWebSocket()

{

var connection = new WebSocket('wss://999.9.9.1:2212');

connection.onopen = function () {

console.log('Connection Opened');

};

connection.onerror = function (error)

{

alert('Please check the server connection: ' + error);

document.getElementById("signData").value=error;

};

connection.onmessage = function (e)

{

if(e.data.indexOf("subProtocol")==-1)

{

alert(e.data);

setData("signData", "signData");

}

{}

};

var completeData = '';

var splitData = [];

var i = 0;

var splitLength = 0;

var j = 0;

var actualData = '';

var textId = '';

var k = 0;

var bufLength = 16300;

alert("Begin");

function setData(txf1, msg)

{

alert("in set Fuction");

msg="action=signpdf\ndatatosign=' '\nsignaction=3\noutputpath=\nsigntype=sign\nexpirycheck=true\ncoordinate=425,100,545,160\nissuername=\ncerttype=ALL\ncertclass=0\npageno=All\ncoSign=true";

actualData = msg;

textId = txf1;

completeData = msg;

//alert('completeData length ' + completeData.length);

if(completeData.length < bufLength)

{

//alert('not splitting');

splitData[0] = msg;

call(txf1,msg);

}

else

{

//alert('splitting');

splitLength = completeData.length / bufLength;

//alert('splitLength ' + splitLength);

var t = 0;

var tt = k + bufLength + 1;

for(i = 0; i < splitLength; i++)

{

splitData[i] = completeData.substring(t, tt);

k = k + bufLength;

t = k + 1;

tt = t + bufLength;

}

call(txf1,msg);

}

}

function call(txf1,msg)

{

if(connection==null)

{

alert('Please check the server connection2');

return;

}

var data="";

var startindex="";

if(msg.length < bufLength)

{

completeData = splitData[0] + 'completed';

connection.send(completeData);

}

else

{

if(j == i-1)

{

completeData = splitData[j] + 'completed';

}

else{

completeData = splitData[j];

}

j++;

connection.send(completeData);

}

connection.onerror = function (error)

{

alert('Please check the server connection: ' + error);

document.getElementById("signData").value=error;

};

connection.onmessage = function (e)

{

if(e.data.indexOf("subProtocol")==-1)

{

if(e.data == 'sendmore')

{

call(textId, actualData);

}

else{

data = data + e.data;

startindex=data.indexOf('completed');

if(startindex != -1)

{

// Client can use the signature

alert(data)

//document.getElementById(txf1).value=data;

splitData = [];

i = 0;

splitLength = 0;

j = 0;

actualData = '';

textId = '';

k = 0;

}

}

}

};

}

}

</script>

</head>

<body>

<p>Click the following button to call websocket</p>

<input type = "button" onclick = "testWebSocket()" value = "Sign

">

</body>

</html>

1 ACCEPTED SOLUTION

Juwin
Active Contributor
0 Kudos

Please have a look at how DEMO_HTML_INPUT program from SAP in SE38. It demos how to get data from HTML page into ABAP.

10 REPLIES 10

Sandra_Rossi
Active Contributor
0 Kudos

What error is it showing? What are you doing to obtain this error? What is your code?

thkolz
Contributor
0 Kudos

Is it an html file on your application server or on cient side?

0 Kudos

We have created the html file in our presentation server. There is a button in the html file against which there is a java script. We need to capture the the response from the java script after the button is pressed. Please advise.

0 Kudos

Thanks a lot. It solved my problem

Juwin
Active Contributor
0 Kudos

Please have a look at how DEMO_HTML_INPUT program from SAP in SE38. It demos how to get data from HTML page into ABAP.

abo
Active Contributor
0 Kudos

From what version is that available? On a 7.31 I have DEMO_HTML_BROWSER only.

Juwin
Active Contributor

Not sure of the version in which this was provided. I have attached the source code here:

REPORT demo_html_input.
CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
  PRIVATE SECTION.
    CLASS-METHODS handle_sapevent
      FOR EVENT sapevent
                  OF cl_abap_browser
      IMPORTING action
                  query_table.
ENDCLASS.
CLASS demo IMPLEMENTATION.
  METHOD main.
    DATA error_list TYPE cl_abap_browser=>html_table.
    SET HANDLER handle_sapevent.
    DATA(html_str) =
       `<html>`
    && `  <head>`
    && `    <meta http-equiv="content-type" `
    && `          content="text/html; `
    && `          charset=utf-8">`
    && `    <script language="JavaScript">`
    && `      function sendInput(form) `
    && `          { fname=form.name;       `
    && `            document[fname].submit();} `
    && `      function InputKeyDown(form) {`
    && `        if(event.keyCode == 13) {`
    && `            fname=form.name;`
    && `            document[fname].submit();} }`
    && `    </script>`
    && `  </head>`
    && `  <body>`
    && `    <form name="INPUT" accept-charset="utf-8" `
    && `          method="post" action="SAPEVENT:INPUT"> `
    && `      <input type="text" id="in1" name="field1" `
    && `             size=30 maxlength=30 title="" value="aaa" `
    && `             onKeyDown="InputKeyDown(this.form);"><br>`
    && `      <input type="text" id="in2" name="field2" `
    && `             size=30 maxlength=30 title="" value="bbb" `
    && `             onKeyDown="InputKeyDown(this.form);"><br>`
    && `      <input type="text" id="in3" name="field3" `
    && `             size=30 maxlength=30 title="" value="ccc" `
    && `             onKeyDown="InputKeyDown(this.form);"><br><br>`
    && `     <button id="enterButton" type="button" `
    && `             title="Enter" onClick="sendInput(INPUT);" `
    && `             onKeypress="if(event.keycode=13) `
    && `             sendInput(INPUT);">`
    && `             Enter</button>`
    && `    </form>`
    && `  </body>`
    && `</html>`.
    cl_abap_browser=>show_html(
      EXPORTING
        html_string = html_str
        title       = 'Input Demo'
      IMPORTING
         html_errors = error_list ).
    IF error_list IS NOT INITIAL.
      MESSAGE 'Error in HTML' TYPE 'I' DISPLAY LIKE 'E'.
    ENDIF.
  ENDMETHOD.
  METHOD handle_sapevent.
    DATA(out) = cl_demo_output_stream=>open( ).
    SET HANDLER cl_demo_output_html=>handle_output FOR out.
    out->write_data( iv_name = 'ACTION'      ia_value = action ).
    out->write_data( iv_name = 'QUERY_TABLE' ia_value = query_table ).
    out->close( ).
  ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
  demo=>main( ).

abo
Active Contributor
0 Kudos

Thanks, but it cannot be activated because of a missing parameter, I guess this is why it was not supplied in our system.

0 Kudos

Thanks juwin.thomas2.

I had tried copying the program demo_html_input. But the parameter html_string is of type string and it was unable to capture the entire html file as the length of my file is quite large approx 5000 characters.

Hence I used the parameter html which is an internal table. But it is not working as expected. Can you please provide a sample program where html table is used and it is capturing the output form the html response.

Juwin
Active Contributor
0 Kudos

String has the capability to take unlimited length. So, 5000 chars shouldn't be any issue. When you say "not working as expected", what do you mean?