0

In one of my views in MVC 4.5 I have a jquery function that performs an ajax post to one of my controller methods. My data is 'stringified' json. For some reason, my controller method always limits my string lengths to 5325 characters. Once the string is sent to the controller, the application throws an error, because the JSON string isn't properly ended.

This is my jquery function:

$(function () {
        $button.click(function () {
            var selected = JSON.stringify($table.bootstrapTable('getSelections'));
            $.ajax({
                type: 'POST',
                url: '/Hub/downloadWorkspaces',
                data: 'selectedSpaces=' + selected,
                success: function () {
                    window.location = '/Hub/browserMultiDownload';
                }
            });
        });
    });

The data is sent to one of my controller methods which is shown below:

    [HttpPost]
    public void downloadWorkspaces(string selectedSpaces)
    {
        System.Diagnostics.Debug.WriteLine(selectedSpaces);
        JArray select = JArray.Parse(selectedSpaces);

        for (int i = 0; i < select.Count; i++)
        {
            string selectedTest = select[i]["Workspace ID"].ToString();
            Hub.Models.Query.getAllFiles(selectedTest);
        }
    }

For some reason, the selectedSpaces string is always cut off. What can I do to fix this?

...,"_Workspace ID_data":{"field":"Workspace ID"},"name":"CM.02.0002.MASPA_Conditional    //No closing brace
ericlund4
  • 11
  • 3
  • It *might* be the `MaxJsonDeserializerMembers` key. See [here](http://stackoverflow.com/questions/20249587/is-it-any-limit-for-post-data-size-in-ajax) – Dave Zych Oct 22 '15 at 15:46
  • Just tried adding it to my web.config, but I'm still getting the same JSON reader exception with my string being cut to 5325 characters. – ericlund4 Oct 22 '15 at 15:54

2 Answers2

1

Try with this demo:

$(function () {
        $button.click(function () {
              var json = {};
              json.id = '101';
              json.name = 'Name test';
            $.ajax({
                type: 'POST',
                url: '/Hub/downloadWorkspaces',
                data: JSON.stringify(json),
                success: function () {
                    window.location = '/Hub/browserMultiDownload';
                }
            });
        });
    });

In the controller:

[HttpPost]
public void downloadWorkspaces(string id, string name)
{
    // id = 101, name = Name test
}
  • I think this suggestion would make request even less valid as there is no longer key name (i.e. "selectedSpaces=") in the request. – Alexei Levenkov Oct 22 '15 at 16:44
  • @AlexeiLevenkov No problem. You can do that by defining an array inside `json`. Like this: `var json = {}; json.keyName = [];`. And in the controller, you can get it by `List keyName` as a parameter. –  Oct 22 '15 at 16:50
0

Most likely data you have includes & which is used to separate values in POST request:

data: "selectedSpaces=" + "{'a':'b&newKey'}", 

request itself will look like folowing and newKey'} is treated as another key in the post with no data:

selectedSpaces={'a':'b&newKey'}

Fix: Url encode (i.e. http://api.jquery.com/jQuery.param/) or send as object:

data: {'selectedSpaces' : selected }

See more info in jQuery.ajax - sending data.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Changing the data from data: 'selectedSpaces=' + selected to data: {'selectedSpaces' : selected } ended up fixing the error. Thanks for the help – ericlund4 Oct 22 '15 at 17:07