on 12-12-2015 1:11 PM
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¶m2=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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
78 | |
10 | |
9 | |
7 | |
6 | |
6 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.