带有Http请求的AngularJS salesforce附件 - 需要的疑难解答帮助

时间:2015-11-12 02:06:08

标签: angularjs ajax httprequest

我在AngularJS服务块中写了这个,我无法让它正常工作:

app.factory('sfAttachment', ['$http', '$q', '$window', function($http, $q, $window){

var attachment = {};


//Save to server function for attachments
attachment.save = function( base64value, mimeType, currentDocTypeId, currentFilename, attachmentId ){

    var data = {
            "Body" : base64value,
            "ContentType": mimeType,
            "ParentId": currentDocTypeId,
            "Name": currentFilename
        };

    var url = $window.__url;
    var method;

    var isUpdate = ($.trim(attachmentId) !== '');
    if (isUpdate) {
        url = url + attachmentId;
        method = 'PATCH';
    } else {
        // Method for creation
        method = 'POST';
    };

    var request = {

        url: url,
        method: method,
        data: data

    };

    var deferred = $q.defer();

    $http(request).then(function(response){
        deferred.resolve(response);
        console.log('file SAVED');
        console.log(response);
    }, function(event){
        deferred.reject('The attachment could not be saved:' + event);
    });


    return deferred.promise;
}


return attachment;}]);
app.run(['$http','$window',function ($http, $window) {

 var sessionId = $window.__sfdcSessionId;

 $http.defaults.useXDomain = true;
 delete $http.defaults.headers.common["X-Requested-With"];
 $http.defaults.headers.common["Access-Control-Allow-Origin"] = "*";
 $http.defaults.headers.common["Accept"] = "application/json";
 $http.defaults.headers.common["content-type"] = "application/json";
 $http.defaults.headers.common['Authorization'] = "OAuth " + sessionId ;
 $http.defaults.headers.common['X-User-Agent'] = "MyClient" ;
}]) ; 

我一直收到这些错误:

  

无法加载资源:服务器响应状态为400(错误请求)

以及:

  

MLHttpRequest无法加载https://youri.cs22.my.salesforce.com/services/data/v26.0/sobjects/Attachment/。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点“https://youri--c.cs22.visual.force.com”访问。响应的HTTP状态代码为400。

我将https://youri--c.cs22.visual.force.com添加到Salesforce中的远程站点设置,但这似乎仍然导致问题......

有什么建议吗?

编辑:我在远程设置>中找到了我需要白名单https://youri--c.cs22.visual.force.com的问题的第一部分。 CORS而不是远程站点,但我仍然收到错误的请求错误...

1 个答案:

答案 0 :(得分:0)

请参阅编辑:

编辑:我想出了我的问题的第一部分我需要白名单https://youri--c.cs22.visual.force.comin远程设置> CORS而不是远程站点,但我仍然收到错误请求错误...