on 05-21-2014 2:37 PM
There is many examples to handle a data, which was send by GET request to on server side JavaScript.
However, until now I could not found one similar example for the POST request processing.
Can anyone provide me a link for such an example?
Kind regards
What specifically about the POST are you having problems with. The main difference I see between GET and POST is the fact that instead of URL parameters, the input is generally in the Request Body. Here is the API call to read the Request Body:
// read request body
var content = $.request.entities[0].body.asString();
Is that what you are looking for?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Thomas,
yes. I don't see my payload at the server side.
after the request:
// request
var formData = JSON.stringify(sap.ui.getCore().byId("projectConfForm").getCustomData()[0].getValue("formValues")); | |||
$.ajax({ |
url: '../services/projectMaintainanceFunctions.xsjs?ACTION=ADD_PROJECT',
type: 'POST',
contentType: 'application/json',
data: formData,
dataType: 'json',
success: function(ajaxResult){},
error: function(jqXHR, textStatus, errorThrown){
sap.ui.commons.MessageBox.show(jqXHR.responseText, "ERROR", "Service call error");
}
});
both $.request.body and $request.entities at the server side are 'undefined'.
Your right - I took that example from a multi-part form POST. Therefore there were multiple entities in that example. In a simpler POST like you have I simply use:
var content = $.request.body.asString();
I created a little test UI:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Test</title>
<script src="/sap/ui5/1/resources/sap-ui-core.js"
id="sap-ui-bootstrap"
data-sap-ui-libs="sap.ui.ux3,sap.ui.commons"
data-sap-ui-theme="sap_bluecrystal" >
</script>
<!-- add sap.ui.table,sap.ui.ux3 and/or other libraries to 'data-sap-ui-libs' if required -->
<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");
}
});
}
var myButton = new sap.ui.commons.Button("btn");
myButton.setText("Hello World Demo");
myButton.attachPress(function(){callService()});
myButton.placeAt("content");
</script>
<script>
</script>
</head>
<body class="sapUiBody" role="application">
<div id="content"></div>
</body>
</html>
I checked in the debugger and it posts just fine and I can see the content:
Hi Thomas,
I using the sap.ui.commons.FileUploader to send data to a XSJS, which should save the picture into a hana NCLOB column.
So far I can select for example a CSV-file with a string contained in it. It is getting posted to the db-table correctly. Until now did not manage to upload PNG-File.
Excerpt: Sending file via Fileloader.
------------------------------------------------------------
var dataUrl = location.origin + "/SAPUI5onHANAapp/services/savePicture.xsjs";
var oFileloader = Fileloader;
var fileName = oFileloader.getValue();
url = dataUrl + "?file_name=" + fileName;
oFileloader.setUploadUrl(url);
oFileloader.upload();
__________________________________
Excerpt: XSJS - get body as String
__________________________________
var file_body = $.request.entities[0].body.asString(); // -> will be inserted into db-table
__________________________________
Do you have any advice, what I need to enhance to save an image successfully to hana? I guess I need to enhance the $request handling. Here my code crashes, when trying to upload a PNG-File.
Thanks and best regards
Tom
Hi Thomas,
In the the example that you have given, you have created an object
oBusinessPartner.PARTNERID = "0000000000";
oBusinessPartner.EMAILADDRESS = "test@sap.com";
and sent to the server thru HTTP Post.
at the server side, to get the request body
var content = $.request.entities[0].body.asString();
How do I get the values for oBusinessPartner.PARTNERID,oBusinessPartner.EMAILADDRESS from the variable content ?
Regards
Veera
User | Count |
---|---|
95 | |
11 | |
11 | |
10 | |
9 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.