Skip to Content

Read body in XSJS

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

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

2 Answers

  • Apr 15, 2016 at 08:50 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • 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

  • Apr 15, 2016 at 10:01 PM

    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());

    Add comment
    10|10000 characters needed characters exceeded

    • 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