cancel
Showing results for 
Search instead for 
Did you mean: 

Failed to execute 'send' on 'XMLHttpRequest'

Jacekkp111
Discoverer
0 Kudos
531

Hello,

I'm working on screen personas and I want to apply an example code to get some extra data on page.

I modify this code and get error. In my opinion I missing the credentials and authentitacion but how to improve it?

Jacekkp111_1-1724939881541.png

 

if (session.idExists('wnd[0]/usr/txtPersonas_168387506699773')) {
if (session.findById('wnd[0]/usr/txtPersonas_168387506699773').text != '') {
var altID = session.findById('wnd[0]/usr/txtPersonas_168387506699773').text;
session.utils.log(altID);
//Create the request object
var http = new XMLHttpRequest();
//Specify the type of request, the url and if the request should be handled
asynchronously or not
http.open(
'GET',
"https://xxxx.xxx.xxx.xxxx/sap/opu/odata4/sap/api_handlingunit/srvd_a2x/sap/handlingunit/0001/Handlin...
itAlternativeID?$filter=Warehouse eq ‘1050’ and EWMHndlgUnitAltvID eq ‘” +
altID +“’”,
false
);
//set request header attributes
http.setRequestHeader(‘DataServiceVersion’, ‘2.0’);
http.setRequestHeader(‘Accept’, ‘application/json’);
//Send the request
http.send();
//Process the response
if (http.readyState == 4 && http.status == 200) {
//The response is available in the http.response property
session.utils.log(http.response);
try {
var HU = JSON.parse(http.response).value[0].HandlingUnitExternalID;
session.utils.log(HU);

session.findById(‘wnd[0]/usr/subX:/SCWM/SAPLRF_INQUIRY_PM:0200/txt/SCWM/S_RF
_SELECTION-RFHU’).text = HU;
} catch (error) {
session.utils.log(‘HU number could not be determined based on alternative HU
ID ‘ + altID);
session.utils.alert(‘HU number could not be determined based on alternative
HU ID ‘ + altID);
}
} else {
session.utils.log(‘OData API query failed’);
session.utils.alert(‘Odata API query failed’);
}
session.utils.log(‘API call finished’);
} else {
session.utils.log(‘API NOT called’);
}
}

Jacekkp111_0-1724939667963.png

 

View Entire Topic
radinator
Participant
0 Kudos

Dear @Jacekkp111 

I looked through your code and I have a few words to say:

  1. Please! For the love of god and your company, do NOT use string concatenation to insert the altID into your URL without validating the content. Or else you have yourself a wonderfull Code Injection. Use the JS tools to remove HTML character like & and single quotes to prevent a SQL Injection.
  2. The part that could be the reason for the crash of your problem could be the inclusion of whitespaces in your URL. As the error message explains, it fails to load the URL.
  3. The URL in the image of the error message and the image/text of your code do not match. I am not talking about the top level domain part, but about the GET parameter. The error message image shows
    $filter=HandlingUnitExtermalID%20eq%20%27000000008%27
    while the text and the image of the code show
    $filter=Warehouse eq '1050' and EWHndlgUnitAltvID eq '" + altID + "'"
    The %20 is the HTML replacement for whitespace while %27 is a single quote.

So to resolve your issue I suggest using a try catch block around the send() function and dig through the stack trace because I don't think it has to do with auth credentials but with the way you construct your URL.

Also please make sure to insert your code in a proper format. The editor of the SAP community forum has a code tag. It makes reading the code much easier 😉

@Giuseppe432Can you please post your code? Or better yet open a new question so we don't have 2 issues within one post 🙂

Kind regards radinator

Jacekkp111
Discoverer
0 Kudos

thank you for your suggestions, I think that the problem in this code at the moment is CORS, do you know how to get around it? access to XMLHttpRequest at 'https://myxxxx-api.s4hana.cloud.sap/sap/opu/odata/sap/API_PROD_ORDER_CONFIRMATION_2_SRV/ProdnOrdConf...' from origin 'https://myxxxx.s4hana.cloud.sap' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. The error message tells us that a web page (likely running JavaScript) at https://myxxxx.s4hana.cloud.sap is trying to make an XMLHttpRequest (a type of HTTP request used by JavaScript) to a different domain: https://myxxxx-api.s4hana.cloud.sap.

radinator
Participant
0 Kudos

@Jacekkp111If your issue is the CORS error message you can avoid this by sending a CORS allow header as described in this SO posting.