AngularJs - `$http` 有效但 `$resource` 无效 - 如何将 `$http` 参数应用于 `$resource`
AngularJs - `$http` works But `$resource` not working - how to apply `$http` params to `$resource`
我正在尝试 post 将图像信息发送到后端。我试过 $resource
但没有用。但是当我尝试使用 $http
时,它似乎有效。但是我已经向 $http
添加了一些附加参数 - 但我不知道如何将这些 params
应用到 $resource
任何人都可以帮助我吗?
$scope.photoData = function (photoData) {
console.log( "photo data", photoData ); //consoles properly
var fileData = photoData[0];
var data = new FormData();
var fileASHXPath = _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/UploadImage/';
//this is not working!
$resource(fileASHXPath).save({ "photoData" : fileData }).$promise.then( function ( response ) {
console.log( response ); //nothing works!
})
//this is works!
var base = _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/UploadImage/';
$http({
url: base + 'TestImage.jpg',
method: 'POST',
processData: false,
headers: { 'Content-Type': undefined },
data: fileData
}
)
.success(function (data) {
if (data.Message.toLowerCase() == 'success') {
console.log(data);
}
})
.error(function (msg, code) {
console.log('error method called');
});
}
更新 - 我的新尝试
还是不行
$resource(base + fileName, {}, {
post:{
method:"POST",
isArray:false,
headers:{'Content-Type':'undefined'}
}
}).$promise.then(function ( response ) {
console.log( "response is", response );
})
尝试调用资源。即在$resource
定义后,调用post()
.
$resource(base + fileName, {}, {
post:{
method:"POST",
isArray:false,
headers:{'Content-Type':'undefined'}
}
}).post().$promise.then(function ( response ) {
console.log( "response is", response );
})
试试这个。即在 $resource 定义之后,用表单数据调用 post()。
$scope.photoData = function (photoData) {
console.log( "photo data", photoData ); //consoles properly
var fd = new FormData();
fd.append("photoData", photoData[0]);
var fileASHXPath = _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/UploadImage/';
$resource(fileASHXPath, {}, {
post: {
method: 'POST',
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
}
}
).post({}, fd).$promise.then(function ( response ) {
console.log( "response is", response );
});
});
我正在尝试 post 将图像信息发送到后端。我试过 $resource
但没有用。但是当我尝试使用 $http
时,它似乎有效。但是我已经向 $http
添加了一些附加参数 - 但我不知道如何将这些 params
应用到 $resource
任何人都可以帮助我吗?
$scope.photoData = function (photoData) {
console.log( "photo data", photoData ); //consoles properly
var fileData = photoData[0];
var data = new FormData();
var fileASHXPath = _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/UploadImage/';
//this is not working!
$resource(fileASHXPath).save({ "photoData" : fileData }).$promise.then( function ( response ) {
console.log( response ); //nothing works!
})
//this is works!
var base = _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/UploadImage/';
$http({
url: base + 'TestImage.jpg',
method: 'POST',
processData: false,
headers: { 'Content-Type': undefined },
data: fileData
}
)
.success(function (data) {
if (data.Message.toLowerCase() == 'success') {
console.log(data);
}
})
.error(function (msg, code) {
console.log('error method called');
});
}
更新 - 我的新尝试
还是不行
$resource(base + fileName, {}, {
post:{
method:"POST",
isArray:false,
headers:{'Content-Type':'undefined'}
}
}).$promise.then(function ( response ) {
console.log( "response is", response );
})
尝试调用资源。即在$resource
定义后,调用post()
.
$resource(base + fileName, {}, {
post:{
method:"POST",
isArray:false,
headers:{'Content-Type':'undefined'}
}
}).post().$promise.then(function ( response ) {
console.log( "response is", response );
})
试试这个。即在 $resource 定义之后,用表单数据调用 post()。
$scope.photoData = function (photoData) {
console.log( "photo data", photoData ); //consoles properly
var fd = new FormData();
fd.append("photoData", photoData[0]);
var fileASHXPath = _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/UploadImage/';
$resource(fileASHXPath, {}, {
post: {
method: 'POST',
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
}
}
).post({}, fd).$promise.then(function ( response ) {
console.log( "response is", response );
});
});