Skip to Content
0
May 24, 2020 at 09:29 PM

Calling SAP CPI End point with Ajax calls

580 Views

Hello Gurus

I have a CPI endpoint API (Groovy3) which converts a JSON to simple text. The API works correctly when invoked using Postman. However when i implement the API using Ajax in UI5 i get a 500 server error. Here's my findings.

Postman results (OK)

Code generated in Postman

Implementation in UI5

Case 1: with no payload (data)

var settings = {

"url": "/http/Groovy3?",

"method": "GET",

"timeout": 0,

"headers": {

"Authorization": "Basic",

"Content-Type": "application/json"

}

};

$.ajax(settings).done(function (response) {

debugger

console.log(response);

});

This code invokes the CPI API correctly but errors out as the payload is missing (Expected result)

Case 2: with payload (data)

var settings = {

"url": "/http/Groovy3?",

"method": "GET",

"timeout": 0,

"headers": {

"Authorization": "Basic",

"Content-Type": "application/json"

},

"data": JSON.stringify({"firstName":"Peter","lastName":"Pan","city":"city123"})

};

$.ajax(settings).done(function (response) {

debugger

console.log(response);

});

on execution i get error 500. Not able to understand what is wrong with my "data" statement in above code. I have checked the syntax which is same as the Postman generated code

The CPI endpoint API does not get invoked also as there is no error message on the CPI side . I am really stuck would appreciate your advise .

Thanks

Kasturi

Attachments

cpi-error1.png (42.4 kB)
cpi-error-2.png (97.2 kB)
cpi-error-3.png (95.7 kB)
cpi-error-4.png (100.7 kB)