# Making AJAX call from within a service

**URL:** <https://forum.webosose.org/t/making-ajax-call-from-within-a-service/885>\
**Category:** App Development\
**Created:** [September 2, 2019, 3:42am UTC](https://forum.webosose.org/t/making-ajax-call-from-within-a-service/885 "2019-09-02T03:42:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![n-jay](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.webosose.org/n-jay/32/126_2.png) [@n-jay](https://forum.webosose.org/u/n-jay)\
**Post date:** [September 2, 2019, 3:42am UTC](https://forum.webosose.org/t/making-ajax-call-from-within-a-service/885/1 "2019-09-02T03:42:05Z")

</div>

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.
