Skip to Content
0

how to call an functoin on XSJS from sapui5?

Jul 19, 2017 at 08:10 PM

94

avatar image
Former Member

Let's say I have a file myFile.xsjs and I have a function myFunction(param){ ...logic inside}

How can I call this function from sapui5 controller? All the examples I've seen follow this format which doesn't really solve my problem.

jQuery.ajax({
    url : "url/to/your/Service.xsjs?" + query,
    success : function(response) {
        // will be called once the xsjs file sends a response
        console.log(response);
    },
    error : function(e) {
        // will be called in case of any errors:
        console.log(e);
    }
});

In this case there is a parameter being passed but not directly to the function. This casse works fine when you have only a few parameters that are single values. I'm going to have several parameters which are arrays, how can I accomplish this?

references
link1, link2

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Sergio Guerrero Jul 20, 2017 at 10:54 AM
1

for your scenario, you need to do an AJAX post from your UI and pass your arrays or other parameters to the XSJS service

on the XSJS side - you get query string params as:

var paramHello = $.request.parameters.get("hello");

and you get the body of your post as:

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

then you can get the properties of your JSON object..

here is the documentation

https://help.sap.com/viewer/52715f71adba4aaeb480d946c742d1f6/2.0.01/en-US/c6bbca35b7734168ac585c0aef9bb527.html#loioc6bbca35b7734168ac585c0aef9bb527__section_N100B3_N1001D_N10001

hope this helps

Show 2 Share
10 |10000 characters needed characters left characters exceeded
Former Member

This doesn't seem to be working for me. When I run it simply doesn't go beyond that line. When I do var body = $.request.body it runs beyond that line but it doesn't bring the data.
Here is my ajax call

$.ajax({
   url: "../path/tomy/file.xsjs",
   type: "POST",
processData: false, contentType: false,
data: myArray, });
0

in your ajax call, your data has to be

data: JSON.stringify(myArray), // make sure you are serializing the object

0