cancel
Showing results for 
Search instead for 
Did you mean: 

Posting data to xsjs not working

jansch
Participant
0 Kudos

Hello all

I am trying to send a data load from UI (SAPUI5) to the backend (xsjs).

Actually trying to use this example:

But its not working!

Here my code:


new sap.m.Button('databutton',{

     text: "send data!",

     press:function(){

          $.post( "/services/test.xsjs",

                            { JSON_DATA: {"Name": "Test"} }

          )

          .done(function(data){

               alert(data);

           });

      }

});

I can see the button but when I click it, nothing happens!

The backend looks like this:


var JSONString = $.request.parameters.get("JSON_DATA");

var JSONObj = JSON.parse(JSONString);

$.response.status = $.net.http.OK;

$.response.setBody(JSONObj.Name);

Where is the problem?

Greetings, Jan

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Did you check the network tab in your browser to find if the request is triggered to the backend ? If yes, can you check what is the response that you get from the request ? Also, have you enabled xcsrf token in your xs app ? If yes, are you passing the token along with the POST request ?

jansch
Participant
0 Kudos

Hi and thanks for your answer.

Actually I got it working. For some reason, the server had a problem with the method I used to send the data to the server.

The following solution works... (whatever the difference is to the solution I used before)


var json = {test: "testcontent"};

var string = JSON.stringify(json);

$.ajax({

    mathod: "POST",

    url: "/services/test.xsjs",

    data: { JSON_DATA : string },

    success: function(data){

        alert(data);

    }

});

Anyone willing to explain me why this works??

Greetings,

Jan

SergioG_TX
Active Contributor
0 Kudos

yes you must do the JSON.stringify function here.

it works because of the way serialization works.. by doing this operation you are serializing the data and thats how the xsjs service expects it (serialized)

alternatively to POST, if you could use a get, then you could pass simple text on the url.. but again this would be a different HTTP method which technically is used for a different reason (read data)

hope this is clear

jansch
Participant
0 Kudos

Alright, then I'll use it that way

Well, the problem is that I really have to pass complex data structures (not just a string like in my example) to the server. And GET is definitely not meant for that. (Plus max data size is at 2KB, where POST is unlimited)

I saw your comment on the blog I linked in my original post. How do I get the body of the request in xsjs if I use POST? And why does it work this way as well even though its POST?

Greetings and many thanks for your explanations.

SergioG_TX
Active Contributor
0 Kudos


for a post request, this is how you would read the body

var reqBody = JSON.parse($.request.body.asString());

Vaibhav_Sapra
Explorer
0 Kudos

Hello All,

I am trying to do the same, but my table has more than say 500 records. I am able to implement the logic using a for loop in xsjs. but it works fine till 200 rows and throws an error when data set is more than 200 rows!

Do you have any idea what can be done here?

Any leads will be appreciated!

Thanks,

Vaibhav Sapra

Answers (0)