简体   繁体   English

$ http.jsonp请求被取消

[英]$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. 我正在构建一个AngularJS应用,该应用将连接到我使用Salesforce创建的自定义公共REST api。 I've tested the REST api via curl and apigee.com and the data is being returned properly. 我已经通过curl和apigee.com测试了REST api,并且已正确返回数据。 The problem occurs when I attempt to call the same endpoint via Angulars $http.jsonp() method. 当我尝试通过Angulars $ http.jsonp()方法调用同一终结点时,会发生问题。 When I attempt to do this the request is canceled, according to Chromes Network monitor. 根据Chromes网络监视器的说法,当我尝试执行此操作时,该请求被取消。

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()... 然后,我通过$ salesforce.login()在我的一个控制器中调用此服务...

Custom Salesforce REST Class: 自定义Salesforce REST类:

@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? 对于我的一生,我无法弄清楚为什么这行不通...有什么想法吗?

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. 原来是因为从沙箱而不是生产服务器提供了Salesforce REST服务,所以Chrome由于证书名称不匹配错误而自动取消了请求。

From the Salesforce Sites Implementation Guide: 从《 Salesforce网站实施指南》中:

Only production organizations have the valid secure.force.com SSL certificates to access sites using HTTPS. 只有生产组织才具有有效的secure.force.com SSL证书,可以使用HTTPS访问网站。 Note: If a site within a sandbox (non-production) organization is accessed using HTTPS, a certificate name mismatch warning may appear 注意:如果使用HTTPS访问沙箱(非生产)组织中的站点,则可能会出现证书名称不匹配警告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM