简体   繁体   English

将GET响应传递给POST

[英]Passing GET response to POST

I am trying to pass multiple maps of data dependent on the id selected to an export to Excel method. 我试图根据选择的ID将多个数据映射传递到导出到Excel方法。

I'm trying to use AngularJS to pass a JSON Array from a GET response to POST using this code: 我正在尝试使用AngularJS使用以下代码从GET响应向POST传递JSON数组:

$scope.exportExcel = function() {
var id='?';
angular.forEach($scope.selection, function(value, key) {
    id = id + "id=" + value + '&';
});

$http.get(searchUrl + id).success(
    function(response) { 
        $http.post(exportUrl, response);
    }
)}

The response of GET will be a JSON Array that looks like so (let's say Jason and Fred were both selected): GET的响应将是一个看起来像这样的JSON数组(假设选择了Jason和Fred):

response = [{'id':'Jason', 'number':1}, {'id':'Fred', 'number':2}]

I have developed the Java code such that the input for the POST method should be like the array above. 我已经开发了Java代码,以便POST方法的输入应类似于上面的数组。

I want the user to select the id's, press the EXPORT button, and immediately get a download dialog. 我希望用户选择ID,然后按EXPORT按钮,然后立即获得一个下载对话框。 No other screen changes or loads. 没有其他屏幕更改或加载。

Is this feasible? 这可行吗? Will my POST command be able to receive the JSONArray response from GET and successfully export? 我的POST命令能否从GET接收JSONArray响应并成功导出?

No reason this won't work. 没有理由,这是行不通的。 Once the GET operation finished and called to its on-success function your code is in a whole new domain and the POST operation will function normally (it doesn't care or know where the data came from). 一旦GET操作完成并调用其on-success功能,您的代码就位于一个全新的域中,并且POST操作将正常运行(它不在乎或不知道数据来自何处)。

Just a quick attempt; 只是一个快速的尝试; Try this (if you can't work with this I'll need more info on what the value looks like -- I don't know the library you're using): 试试这个(如果您不能使用它,我将需要更多有关值的信息-我不知道您使用的库):

$scope.exportExcel = function() {
var id;
angular.forEach($scope.selection, function(value, key) {
    id = (id?id+"&":"?")
       + "id["+key+"]=" + encodeURIComponent(value.id)
       + "&number["+key+"]=" + value.number;
});

$http.get(searchUrl + id).success(
    function(response) { 
        $http.post(exportUrl, response);
    }
)}

Yes, it works. 是的,它有效。 I just needed affirmation after all. 毕竟我只需要确认即可。 I tested it and it works. 我对其进行了测试,并且可以正常工作。 Thanks for those who answered. 感谢那些回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM