AngularJS 带有 Http 请求的 salesforce 附件 - 需要故障排除帮助
AngularJS salesforce attachment with Http request - Troubleshooting help needed
我在 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" ;
}]) ;
我不断收到这些错误:
Failed to load resource: the server responded with a status of 400 (Bad Request)
还有这个:
MLHttpRequest cannot load https://youri.cs22.my.salesforce.com/services/data/v26.0/sobjects/Attachment/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://youri--c.cs22.visual.force.com' is therefore not allowed access. The response had HTTP status code 400.
我已将 https://youri--c.cs22.visual.force.com
添加到 Salesforce 中的远程站点设置,但似乎仍然会导致问题...
有什么建议吗?
编辑:我想出了问题的第一部分,我需要在远程设置> CORS 中将 https://youri--c.cs22.visual.force.com
列入白名单,而不是远程站点,但我仍然收到 Bad request 错误。 .
查看编辑:
编辑:我想出了问题的第一部分,我需要将 https://youri--c.cs22.visual.force.comin 远程设置> CORS 而不是远程站点列入白名单,但我仍然收到 Bad request 错误...
我在 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" ;
}]) ;
我不断收到这些错误:
Failed to load resource: the server responded with a status of 400 (Bad Request)
还有这个:
MLHttpRequest cannot load https://youri.cs22.my.salesforce.com/services/data/v26.0/sobjects/Attachment/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://youri--c.cs22.visual.force.com' is therefore not allowed access. The response had HTTP status code 400.
我已将 https://youri--c.cs22.visual.force.com
添加到 Salesforce 中的远程站点设置,但似乎仍然会导致问题...
有什么建议吗?
编辑:我想出了问题的第一部分,我需要在远程设置> CORS 中将 https://youri--c.cs22.visual.force.com
列入白名单,而不是远程站点,但我仍然收到 Bad request 错误。 .
查看编辑:
编辑:我想出了问题的第一部分,我需要将 https://youri--c.cs22.visual.force.comin 远程设置> CORS 而不是远程站点列入白名单,但我仍然收到 Bad request 错误...