Forum

Making AJAX call from within a service

Hi All,

To provide some context, I've been attempting to make an AJAX (GET) call from within a service.
While the call is successful when made without relying on the service (such as via a button trigger), it fails when made through the service.

Below is the code segment I've used for invoking the service;

function activateBackgroundService(accessToken, serverEndpoint) {
    	var subscribeStatus = true;
        var resubscribeStatus = true;
        
        var request = webOS.service.request("luna://com.entgra.app.controlservice/", {
            method:"operation",
            parameters: {  
                msg:accessToken,
                endpoint:serverEndpoint
            },
            onSuccess: function(inResponse) {  
                console.log(inResponse);  
            },
            onFailure: function(inError) {  
            	console.log("Fail " + inError.errorText);  
            },
            onComplete: function(inResponse) {  
                //....  
            },
            subscribe: subscribeStatus,
            resubscribe: resubscribeStatus
        });
    }

Here is the code I've used to register the service;

service.register("operation", function(message) {
	var counter = 0
	setInterval(operationRequest, 10000)
	
	function operationRequest() {
		console.log("In hello callback");
		
		$.ajax({
            type: "GET",
            url: message.payload.endpoint + "/api/device-mgt/v1.0/device/agent/pending/operations/webOS/18",
            headers: {
            	'Authorization': 'Bearer ' +  message.payload.msg,
                'Content-Type': 'application/json'
            },
            success: function (resp) {
            	message.respond({
        			returnValue: true,
        			message: resp
        		});
            },
            error: function (xhr) {
            	message.respond({
        			returnValue: false,
        			message: "Fail" + JSON.parse(xhr)
        		});
            }
        });
		
	}
});

Through testing, I've figured out that the parameters are passed properly, the service gets initiated as intended and as mentioned earlier; the AJAX call is also successful.

However, what keeps happening is that failure state in the service invocation is triggered and a quick debug shows that the errorText proclaims "Message status unknown."

Would appreciate some suggestions on how to fix this.