-2

I want to simple file download method.

My class is:

public class TestClass
{
    public string StringParam { get; set; }
    public int IntParam{ get; set; }
}

My Web Method:

public void MyExport(TestClass exportArg)
{
     //do something
}

I can pass javascript object to web method as TestClass object with ajax

var params = {};
params['StringParam '] = 'asd';
params['IntParam'] = 5;

$.ajax({
            type: 'POST',
            url: 'Home/MyExport',
            async: false,
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(params)
        });

But when i try this with window.open exportArg parameter is null. How can i pass a javascript object to web method with window.open

window.open('Home/MyExport?exportArg=' + JSON.stringify(params));
Onur
  • 1

1 Answers1

0

Your method is expecting a POST request, by appending the value onto the query string and calling window.open you're executing a GET request.

You cant execute a POST with window.open. There are a few workarounds, you can find them in this question & answers: Window.Open POST

Community
  • 1
  • 1
Jamiec
  • 133,658
  • 13
  • 134
  • 193