【问题标题】:AngularJS salesforce attachment with Http request - Troubleshooting help needed带有 Http 请求的 AngularJS 销售人员附件 - 需要故障排除帮助
【发布时间】:2015-11-12 02:06:08
【问题描述】:

我在 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”标头。因此,Origin 'https://youri--c.cs22.visual.force.com' 不允许访问。响应的 HTTP 状态代码为 400。

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

有什么建议吗?

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

【问题讨论】:

    标签: angularjs ajax httprequest


    【解决方案1】:

    查看编辑:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 2020-07-01
      相关资源
      最近更新 更多