0

EDIT taken from OP comment:

"we're using the classic SAPGui [ed: not the SAPGui for HTML]"

I'd like to send a request to a simple URL from my JavaScript, so that the base URL will NOT be added to the request URL. For example, the request should be sent to the following URL (without the base URL): SAPEVENT:SOME_TEXT?2

I used the jQuery's $.ajax function in order to implement it, but without success.

Here is a JSFiddle for it: http://jsfiddle.net/txb6tdjj/2/

The JS code:

function sendEvent(id) {
    $.ajax("SAPEVENT:SOME_TEXT?" + id);
}    
sendEvent(2);

I see the following error in the JS console:

XMLHttpRequest cannot load sapevent:SOME_TEXT?2. Cross origin requests are only supported for HTTP. (jquery-2.1.0.js:8556)

I even set the parameter crossDomain: true, but it didn't help: http://jsfiddle.net/auhx2v2v/3/

The JS code:

function sendEvent(id) {
    $.ajax({
        url: "SAPEVENT:SOME_TEXT?" + id,
        crossDomain: true
    });
}
sendEvent(2);

It ends up with the same error.

It works correct in the HTML like this: http://jsfiddle.net/1f6npcn2/2/

The HTML code which works correctly:

<FORM action="SAPEVENT:PRESS_ME">
    <A HREF="SAPEVENT:CLICK_ON_ME">Click on me to send an event!</A> 
    <INPUT TYPE="submit" VALUE="Press me to send an event!"/>
</FORM>

But I need to implement it in JavaScript, so that a request parameter can be set dynamically in the URL in JavaScript. Do you know how to implement it in JS so that the request will be sent to the URL SAPEVENT:SOME_TEXT?2 without the base URL?

Additional information about used browsers: The error is shown only in Chrome. IE and Firefox do not show an error, but they also don't send the request.

Additional information for the SAP guys: I know there is a SAP Note 191908 which states that it's impossible, but a colleague has confirmed that he has successfully tested such functionality in an HTML page which used the same code as I copied above (see the HTML code above and http://jsfiddle.net/1f6npcn2/2/). So the SAP Note is wrong. I know how I can implement this functionality in HTML, but I don't know how I can implement it in JS. That's the problem.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
krm
  • 2,074
  • 2
  • 18
  • 21
  • If you start URL with "/" it will attach it to current domain. $.ajax("/SAPEVENT:SOME_TEXT?123") will request http://yourdomain.com/SAPEVENT:SOME_TEXT?123 if you're currently on http://yourdomain.com – Elena Sharovar Sep 09 '14 at 19:44
  • But I didn't start the URL with "/" and I do NOT want to have the base URL in the request URL, i.e. I don't want yourdomain.com/SAPEVENT:SOME_TEXT?123, but I want only exactly SAPEVENT:SOME_TEXT?123 as the request URL. It works in case of the HTML example from my question, but not in the JS. – krm Sep 09 '14 at 20:21

4 Answers4

1

I have no experience of working with SAP but I think you are missing a crucial part here.

In the samples you gave SAPEVENT:CLICK_ON_ME isn't a http url at all but rather it would invoke whatever handles the SAPEVENT-protocol on the local computer with the parameter CLICK_ON_ME. I'm guessing that you have some sort of client installed on your computer that does this for you (how do I create my own URL protocol? (e.g. so://...) contains some more information on how this is accomplished).

The reason your error-message talks about crossdomain-stuff is probably because it tried to interpret it as host:port.

So in other words, since this isn't a http url there isn't a webserver working on the other end so you can't do ajax-requests against it.

Community
  • 1
  • 1
Karl-Johan Sjögren
  • 16,544
  • 7
  • 59
  • 68
  • I understand what you mean, but the HTML example from my question works: [http://jsfiddle.net/1f6npcn2/2/](http://jsfiddle.net/1f6npcn2/2/). It sends a request which contains exactly only "SAPEVENT:PRESS_ME" as the request URL. So why I can't implement it in JS? – krm Sep 09 '14 at 20:24
1

The SAPEVENT: stuff is not handled by any web server. The SAP GUI uses an embedded Internet Explorer and registers a custom protocol handler. There is no use in trying to use ajax techniques since you need to reach the container of the client, not the server. To reiterate: You do not want to "send a request" anywhere, you want to convince the browser that a certain local navigation event happened". SAP Note 191908 contains more information on that topic.

vwegert
  • 18,371
  • 3
  • 37
  • 55
  • Thanks for clarifying, having 0 experience on SAP i was assuming SAP delivered to browsers, not only to their own client so i thought this was a piece of code that needs to be parsed by a webserver. – wiesion Sep 09 '14 at 20:54
  • @vwegert: I already know the SAP Note mentioned by you. I can't access the link provided by you, because it requires a login, but here is a [public link](http://www.stechno.net/sap-notes.html?view=sapnote&id=191908). However this note is related to the SAPGui for HTML, but we're using the classic SAPGui. A colleague has successfully tested such case using an external HTML page, as shown in the HTML example which is included in my question and works correctly. I know how to make it work in an HTML page, but I can't make it work in JS. – krm Sep 09 '14 at 21:00
0

No idea about SAP Views, but to me this seems like a usual behaviour on webservers. I presume that SAPEVENT gets parsed by the server during the runtime to a more regular URI. Only the views get parsed, not the resources like CSS and JS, so the SAPEVENT placeholders in the JS file don't get parsed and the JS interpreter will not accept it as a valid URI. One of the common ways of solving this, is to create either a hidden form in the HTML or just a hidden input containing the server-generated values you are needing. For example

SAP View:

<input type="hidden" id="my_event_url" value="SAPEVENT:PRESS_ME">

JS:

function sendEvent(id) {
    $.ajax({
        url: $('#my_event_url').val() + '?' + id,
        crossDomain: true
    });
}
sendEvent(2);
wiesion
  • 2,349
  • 12
  • 21
  • This solution didn't work. It doesn't change the way the URL is passed. I tried it in multiple different versions, i.e. also with some other small modifications, but it didn't work. I get the same error as mentioned in my question. – krm Sep 09 '14 at 21:18
  • I see, i took it from vwegert's answer that the SAP client is an embedded IE and works with a custom protocol, was already wondering about the unusual URI's. What does `alert(window.location.protocol)` in JS return? – wiesion Sep 09 '14 at 21:32
  • If that returns something else than 'SAPEVENT' then the XHR restrictions take effect. You could probably work around this by using IE's own `XDomainRequest` Object (not jQuery's `.ajax()` object, but i have no idea how user authentication on the SAPGui is done, you probably would need to copy all of the request headers into the XDRequest. If that doesn't work or is not an option for you, try to implement an ` – wiesion Sep 09 '14 at 21:48
  • I already implemented a working solution - see my answer. As far as your question is concerned and as far as I know, the web page in SAPGui is displayed inside of an iframe. I suppose it receives the SAP events. My only task was to send the SAP events. I don't have to care how they will be processed by the SAPGui. – krm Sep 09 '14 at 22:15
0

I finally implemented it in JavaScript. Thanks go to this web page. I modified the solution which was shown in this web page in order to add a link instead of a form in JavaScript. This is the working solution in JS:

var targetUrl = "SAPEVENT:SOME_TEXT?2";

function sendSapEvent(targetUrl) {
    var link = document.createElement("a");
    link.setAttribute("style", "display:none;");
    link.setAttribute("href", targetUrl);

    // Move the click function to another variable
    // so that it doesn't get overwritten.
    link._click_function_ = link.click;

    document.body.appendChild(link);
    link._click_function_();
}

sendSapEvent(targetUrl);

You can find it also in this JSFiddle: http://jsfiddle.net/708r95p0/6/

It works! It sends a request to the URL sapevent:SOME_TEXT?2

I decided to use a link instead of a form element, bacause I couldn't pass the request parameter using a form.

krm
  • 2,074
  • 2
  • 18
  • 21