cancel
Showing results for 
Search instead for 
Did you mean: 

Processing HTTP POST request on HANA xsjs

former_member184970
Active Participant
0 Kudos

Hi,

     I would like to know how to handle a HTTP POST request in an XSJS file.I have a form with many input fields and I would like call an HANA xsjs file to process the data after user clicks on a submit button.

     In case of a HTTP GET method, one can append the values of the form parameters as query strings and pass it in the URL.such as below.

            /path/<name of the xsjs>.xsjs?param1=val1&param2=val2.

     on the server side,the values can be retrived as,

         var sel_param1 = $.request.parameters.get("param1");

     Now,since  have to pass the values thru HTTP POST,I referred to the below thread

        http://scn.sap.com/thread/3557509  In this method,it is explained how to to pass the parameters in HTTP Post. For this, a javascript object is created and sent via $.ajax method as below.

<script type="text/javascript">

     function callService(){

  var oBusinessPartner  = {};

  oBusinessPartner.PARTNERID = "0000000000";

  oBusinessPartner.EMAILADDRESS = "test@sap.com";

  $.ajax({

                 url: '/playground/test/post.xsjs?ACTION=ADD_PROJECT',

                 type: 'POST',

                 contentType: 'application/json',

                 data: JSON.stringify(oBusinessPartner),

                 dataType: 'json',

                 success: function(){alert("Success")},

                 error:  function(jqXHR, textStatus, errorThrown){

                     sap.ui.commons.MessageBox.show(jqXHR.responseText, "ERROR", "Service call error");

                 }

             });

     }

On XSJS side,the the the value is retrived as below,

          var content = $.request.body.asString();

My question is,how to obtain the individual parameter values such as PARTNERID,EMAILADDRESS from the variable content.

Regards

Veera

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Veera,

I was trying different combinations of contenType and some other ways.

Finally, I managed to get my content parsed, by adding this:

var content = $.request.body.asString();

content = JSON.parse(content);



content[0].PARTNERID will contain the individual parameter info.

check it by setting the body of your response as

$.response.setBody(content[0].PARTNERID);


and it should work as expected.


Also, to process it in a loop, you may write a Loop statement on content.length.


Hope this helps (if you have not solved it already!!)

Regards,

Pratik