AngularJS $resource 正在将整个对象编码为 URL
AngularJS $resource is encoding entire object into URL
我正在使用 Angular 1.4.8 与 Angular UI 和 TypeScript。我的模型是这样定义的:
export interface IBatch extends ng.resource.IResource<IBatch> {
id: Number;
...
}
export interface IBatchResource extends ng.resource.IResourceClass<IBatch> {
snapshot(batch: IBatch);
}
我使用自定义 HTTP 动词 TAKE-SNAPSHOT
设置我的 Batch
资源,其中 returns 是 200 OK
或 404 NOT FOUND
:
var paramDefaults = {
id: '@id'
};
var actions = {
'snapshot': { method: 'TAKE-SNAPSHOT' }
};
return <IBatchResource> this.$resource('/api/batches/:id', paramDefaults, actions);
这让我可以拍摄特定批次的快照。此 API 调用的 only 参数是批次 ID。但是,$resource
正在将 整个 Batch
对象编码到查询字符串中(实际字符串长度超过 1000 个字符,为简洁起见缩短):
localhost:15000/api/batches/4?$originalData=%7B%22id%22:4,%22createdDateUtc%22:%222015-12-...
如何让 $resource
将请求定向到 localhost:15000/api/batches/4
?
我设法解决了:
var paramDefaults = {
id: '@id'
};
var actions = {
'snapshot': <ActionDescriptor>{ method: 'TAKE-SNAPSHOT' }
};
var retval = <IBatchResource> this.$resource('/api/batches/:id', paramDefaults, actions);
/*
* Passing in the object results in serializing the entire Batch into the URL query string.
* Instead, we only want to pass the ID.
*/
var oldSnapshotFn = retval.snapshot;
retval.snapshot = (batch: IBatch) => oldSnapshotFn(<any>{id: batch.id });
return retval;
我正在使用 Angular 1.4.8 与 Angular UI 和 TypeScript。我的模型是这样定义的:
export interface IBatch extends ng.resource.IResource<IBatch> {
id: Number;
...
}
export interface IBatchResource extends ng.resource.IResourceClass<IBatch> {
snapshot(batch: IBatch);
}
我使用自定义 HTTP 动词 TAKE-SNAPSHOT
设置我的 Batch
资源,其中 returns 是 200 OK
或 404 NOT FOUND
:
var paramDefaults = {
id: '@id'
};
var actions = {
'snapshot': { method: 'TAKE-SNAPSHOT' }
};
return <IBatchResource> this.$resource('/api/batches/:id', paramDefaults, actions);
这让我可以拍摄特定批次的快照。此 API 调用的 only 参数是批次 ID。但是,$resource
正在将 整个 Batch
对象编码到查询字符串中(实际字符串长度超过 1000 个字符,为简洁起见缩短):
localhost:15000/api/batches/4?$originalData=%7B%22id%22:4,%22createdDateUtc%22:%222015-12-...
如何让 $resource
将请求定向到 localhost:15000/api/batches/4
?
我设法解决了:
var paramDefaults = {
id: '@id'
};
var actions = {
'snapshot': <ActionDescriptor>{ method: 'TAKE-SNAPSHOT' }
};
var retval = <IBatchResource> this.$resource('/api/batches/:id', paramDefaults, actions);
/*
* Passing in the object results in serializing the entire Batch into the URL query string.
* Instead, we only want to pass the ID.
*/
var oldSnapshotFn = retval.snapshot;
retval.snapshot = (batch: IBatch) => oldSnapshotFn(<any>{id: batch.id });
return retval;