Passing JSON to a server, such as GET / POST, to receive a file instead of ajax response

I have an object created in javacript with a lot of data, and I serialize it in JSON to send it to the server. After that, the server should do some work and create a dynamic file so that it can be downloaded. For the last procedure, I created ASHX, but can be changed. I already get "httpcontext", which I found in another question, how to work with it to get data from JSON, so my question is not related to this.

The problem (more JS-oriented) is this:

How can I send JSON to ASHX as a URL / GET / POST for a common handler to avoid the “ajax response” and to open the user a new window with dynamically generated link?

Thank you, sorry for my English (please edit) and good wishes!

Note 1: I can’t use the code of the third part
Note 2: I can’t use JSON.NET
Note 3: I can’t save the report on the server, so the answer must be a generated file for downloading, especially since the download itself is the server’s response.

--- UPDATE: ----

I read this question: Is it possible to host JSON without using AJAX?

The only thing I don’t understand from this question is how to make it work, thinking that I have a “download link”

+4
source share
2 answers

Finally, I solved the problem with this (correctly).

json- POST , javfascript

var dataToPostInExport = JSON.stringify(queryToVerify);
//Convert To POST and send  
var VerifyForm = document.createElement("form");
VerifyForm.target = "_blank";
VerifyForm.method = "POST";
VerifyForm.action = "file.ashx";
var dataInput = document.createElement("input");
dataInput.type = "hidden";
dataInput.name = "mydata";
dataInput.value = dataToPostInExport;
VerifyForm.appendChild(dataInput);
document.body.appendChild(VerifyForm);
VerifyForm.submit();

ashx:

            Dim DataToParse As String
            DataToParse = HttpContext.Current.Request.Form("mydata")
            Dim JSSerializer As New JavaScriptSerializer
            Dim QueryToExport as my very own type!
            QueryToExport = JSSerializer.Deserialize(Of My Own Type)(dataToParse)
0

, , .

1) Ajax-load iframe, aspx, .

2) aspx .

3) Response .

+2

All Articles