jonnybro
jonnybro

Reputation: 312

$http.jsonp requests are gettings canceled

I swear this was working earlier, took a break, and now its not working again...

I'm building an AngularJS app that connects to a custom public REST api I've created with Salesforce. I've tested the REST api via curl and apigee.com and the data is being returned properly. The problem occurs when I attempt to call the same endpoint via Angulars $http.jsonp() method. When I attempt to do this the request is canceled, according to Chromes Network monitor.

Angular Factory:

angular.module('SalesforceService', [], function($provide){

    $provide.factory('$salesforce', function($http, $q){

        return {
            login: function(email, password){

                var endpoint  = 'https://merchant.dev1.cs15.force.com/freelance/services/apexrest/FreelanceService?name=Jonathan&callback=JSON_CALLBACK';
                var deferred = $q.defer();

                console.log('...calling salesforce...');
                $http.jsonp(endpoint)               
                .success(function(data){
                    console.log('--SALESFORCE RESPONSE:');
                    console.log(data);

                    deferred.resolve(data);
                }).error(function(data, status, headers, config){
                    console.log('---SALESFORCE ERROR:');
                    console.log(data);
                    console.log(status);
                    console.log(headers());
                    console.log(config);

                    deferred.reject('An error occurred when attempting to login.');
                });

                return deferred.promise;
            }
        };

    });

})

I then call this service in one of my controllers via $salesforce.login()...

Custom Salesforce REST Class:

@RestResource(urlMapping='/FreelanceService')
global class FreelanceService 
{
    global class TestObject{
        public String message;

        public TestObject(String message){
            this.message = message;
        }
    }

    @HttpGet
    global static void goGet()
    {
        String name = RestContext.request.params.get('name');

        TestObject o = new TestObject('Hello ' + name);

        RestContext.response.addHeader('Content-Type', 'application/json');
        RestContext.response.responseBody = formatResponse(o);
    }

    public static blob formatResponse(TestObject obj)
    {
        //instantiate string to be returned
        String response;

        //get callback parameter from url
        String callback = RestContext.request.params.get('callback');

        //return JSON wrapped in function if a callback parameter exists
        if(callback!=null)
        {
            response = callback + '('+JSON.serialize(obj)+')';
        }
        else
        {
            response = JSON.serialize(obj);
        }

        return blob.valueOf(response);
    }
}

For the life of me I cannot figure out why this isn't working... Any ideas?

Upvotes: 0

Views: 425

Answers (1)

jonnybro
jonnybro

Reputation: 312

Turns out because the Salesforce REST service was being served from a Sandbox instead of Production, Chrome was automatically canceling the request because of a certificate name mismatch error.

From the Salesforce Sites Implementation Guide:

Only production organizations have the valid secure.force.com SSL certificates to access sites using HTTPS. Note: If a site within a sandbox (non-production) organization is accessed using HTTPS, a certificate name mismatch warning may appear

Upvotes: 1

Related Questions