0
votes

I'm trying to make a simple REST GET request to salesforce Live Agent API to check whether service is available.

I can make the simple GET request successfully from my Web based REST client (Chrome Plugin - Boomerang) but am unable to successfully make the request from JQuery $.ajax.

I am getting a 400 Bad Request error.

$(document).ready(function(){
     checkAvailable("https://d.xxx-xx-xxx.salesforceliveagent.com/chat/rest/Visitor/Availability?org_id=xxxxxxxxxxxxxxx&deployment_id=xxxxxxxxxxxxxxx&Availability.ids=[xxxxxxxxxxxxxx]");
});

function checkAvailable(URL)
{
    $.ajax({
        url: URL,     
        type: "GET",
        beforeSend: function(xhr){
            xhr.setRequestHeader('X-LIVEAGENT-AFFINITY', "null");
            xhr.setRequestHeader('X-LIVEAGENT-API-VERSION', "41");
            },
        success: function(response) { alert('Success: ' + response); },
        error: function(req, err){ console.log('Error Message: ' + err); }
     });

}

Can anyone shed some light on what I might be doing wrong ? I suspect the issue is with the data format but my Rest client has no issues with it.

1
Could it be a cross-domain issue? - Roamer-1888
It works from a Chrome plugin REST client. - Craig
OK, I get that but maybe the plugin and the browser are not identical with regard to cross-domain constraints? It's worth checking. - Roamer-1888
you can start debugging by using your browser tools, or a tool like Fiddler, to examine the full request made by your code, and compare what you send to what your REST client sends, and play spot the difference. - ADyson

1 Answers

0
votes

The answer lies in the Live Agent REST API CORS settings according to Saleforce support. I haven't tried the solution yet, but it seems plausible. Basically, you cannot conduct an AJAX query from the browser due to CORS restrictions on their REST API. You need to whitelist the domain.

They have referenced this article...

https://developer.salesforce.com/docs/atlas.en-us.chatterapi.meta/chatterapi/extend_code_cors.htm

The work around I ended up using prior to them eventually, after 2 weeksm, getting back to me with a reply, was to create a PHP proxy to make the call to Live Agent REST API.

Basically, my AJAX code, calls the PHP Proxy file on my server, which then calls the Live Agent REST API via curl and then send the response back to the Ajax request thus avoiding the CORS issue.