cancel
Showing results for 
Search instead for 
Did you mean: 

Read body in XSJS

Chad_He
Participant
0 Kudos

Dear Experts,

I post a request in fiori with following code:


addJob: function () {

  var oJsonData = {

  "JOBID":"JB9006",

  "DESCRIPTION":"AddJob9006",

  "DEPARTMENT":"AddDep.",

  "VALID_TILL":Date.now()

  };

  $.ajax({

  url:this.getView().getModel().sServiceUrl.substr(0,35)+"/update.xsjs?$mode=ins",

  type:'POST',

  contentType:'application/json',

  data: JSON.stringify(oJsonData),

  dataType:'json',

  success: function(){alert("Update Success!");}

  });

  }

But in update.xsjs, I can't read body with follow command:


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

It's always undefined.

How to read body of the request?

By the ways, develop environment of xsjs is HCP Trial.

Thanks & Regards.

Chad

Accepted Solutions (0)

Answers (2)

Answers (2)

SergioG_TX
Active Contributor
0 Kudos

Chad,

I think your issue is that you may be using the incorrect url.. or may need to consider a relative path to your xsjs service.

also, I noticed you are posting an object but in your url you are also passing a query string... you should be able to get the json string as you have in your code.. if you need to conver to a json object.. then do

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

Chad_He
Participant
0 Kudos

Hi Serigo,

I tried to remove "$.request.body", the url can return a 200 response, so I think the URL is correct.

And I tried the suggested command, but not ok.

You said the "query string" means "?$mode=ins"? I just want to set a parameter to the request. Is it not allowed in xsjs syntax?Or it's not allowd to set parameter if I set request body?

I also tried to change JSON Object to String, but it's still not correct.

Chad

pfefferf
Active Contributor
0 Kudos

Hello Chad,

your coding looks ok (although with that little crazy URL determination ).

Did you check if your request has the payload?

Does the "undefined" message really occur for the $.request.body object or do you have further coding which could lead to the error?

Regards,

Florian

Chad_He
Participant
0 Kudos

Hi Florian,

I'm a new hand on XSJS, so I've no idea how to check whether the request is payloaded or not. Could you give me some information about it?

I try to remove the "$.request.body", and the response will be 200 ok.

But if I add $.request.body to code, the response will be 500 error.

So I believe the reason is the command "$.request.body".

By the way, I tried the following code, and debuged it, I found that $.response.body is undefined, but there is content in real response. I don't know if there is some relationship between the two things.


$.response.setBody("blablabla");

Chad

pfefferf
Active Contributor
0 Kudos

Hi Chad,

to check the payload of the request open the browser developer tools (F12), go to the network tab, do the action which triggers the post request and then check the payload.

On the other side in the XSJS in the debugger mode the body has to be the same than the payload.

As you can see I have implemented your little example and I works w/o issues. If have tried it from an UI5 app running on XS and an UI5 app running on HCP consuming the XSJS service via a destination.

Best Regards,

Florian

Chad_He
Participant
0 Kudos

Hi Florian,

I test it via destination, and it's seems like that there is no content in request.body.But there is content in payload.


And I'd like to know how did you debug a POST request? My POST can't trigger break point. I can only use browser to trigger break point by execute db address url.


My DB direct url is:

https://bootcamphanausernametrial.hanatrial.ondemand.com/bootcamp/update.xsjs


The request in frontend app is:

https://webidetesting6739963-usernametrial.dispatcher.hanatrial.ondemand.com/destinations/BootCampHA...


Chad

pfefferf
Active Contributor
0 Kudos

I debugged it called from a UI5 app running in XS. But I tried it also from a HCP UI5 app via a destination with success. Can you show your whole xsjs coding if there is more than you have already posted.

Chad_He
Participant
0 Kudos

Hi Florian,

XSJS File:


function updateData() {

  if ($.request.parameters.get("$mode") === "ins") {

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

  var oJson = JSON.parse(sData);

  var squery = 'INSERT INTO "SYSTEM"."bootcamp.DB::JobModel.Job" VALUES (?,?,?,?,?,?)';

  var connection = $.hdb.getConnection();

  connection.executeUpdate(squery,oJson.JOBID,oJson.DESCRIPTION,oJson.DEPARTMENT,oJson.VALID_TILL,'','');

  connection.commit();

  connection.close();

     $.response.contentType = "text/html";

     $.response.setBody("Success");

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

  } else {

     $.response.contentType = "text/html";

     $.response.setBody("Wrong Mode!");

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

  }

}

updateData();

Database:

Database:

Thanks,

Chad

SergioG_TX
Active Contributor
0 Kudos

Chad,

I see a few things here :

1) do not include $ to get the parameter

2) your response type - use html if you are returning a valid html response, otherwise I suggest to respond with json as you are sending json into your xsjs service

here is a modified version of your code - hope it works (for some reason I couldn't format it) but you can on your side

function updateData() { var body = { response : ''};         if ($.request.parameters.get("mode") === "ins") {      var sData = $.request.body.asString();      var oJson = JSON.parse(sData);      var squery = 'INSERT INTO "SYSTEM"."bootcamp.DB::JobModel.Job" VALUES (?,?,?,?,?,?)';      var connection = $.hdb.getConnection();      connection.executeUpdate(squery,oJson.JOBID,oJson.DESCRIPTION,oJson.DEPARTMENT,oJson.VALID_TILL,'','');      connection.commit();      connection.close();           body.response = "Success";          } else {       body.response = "Wrong Mode!";   }      $.response.setBody(JSON.stringify(response));   $.response.status = $.net.http.OK;   $.response.contentType = ""application/json"";  // I would recomment to return json and use the "application/json" as the content type, otherwise you need to set valid html to use html type response }  updateData();

Chad_He
Participant
0 Kudos

Hi Sergio,

Thank you for your suggestion.

While, the issue is not response body, but request body.

It's still can't get body of request.

I think it's maybe because of something of HCP trail.

Could you have a look of my .xsaccess? Whether the access file is ok or not?


{

    "exposed": true,

    "authentication": [{

        "method": "Form"

    }],

    "mime_mapping": [{

        "extension": "jpg",

        "mimetype": "image/jpeg"

    }],

    "force_ssl": false,

    "enable_etags": true,

    "prevent_xsrf": false,

    "anonymous_connection": null,

    "cors": [{

        "enabled": false,

        "allowMethods": ["GET","POST","DELETE","PUT"]

    }],

    "cache_control": "no-cache, no-store",

    "default_file": "jobs.xsjs"

}

Thanks,

Chad

SergioG_TX
Active Contributor
0 Kudos

Chad,

it seems like you have the correct info on your xsaccess file... you may not need to have all the setting you put there but if you need them all, it doesn't affect the output.

have you tried checking on a simpler set up? like a hello world example with no authentication? then build on top of that... I think you may be doing too much at once... start on simple and build from there... sorry this is not a definite answer you need but it may help

good luck

Chad_He
Participant
0 Kudos

Hi Sergio,

Really thank you for your suggestion.

I tried to simplify .xsaccess and rebuild the project from "Hello World".

It's correct at first, but when I move the code source to destination the issue occurred again.

I can only think it's issue of HCP Trial.

Again, I'm very appreciated for your answer.

Chad