1

I'm working on a Asp.net MVC application. In my project, I'm using a third party JavaScript library called Dhtmlx Scheduler.

There is a function in this library then writes all the data on the scheduler into XML format. I then need to manipulate this data, and write in back onto a <textarea> on my view page.

as of now this is what I have:

View:

function save() {
    var url = "/Home/Save"

    var xml = scheduler.toXML();

    $.ajax({
        url: url,
        Type: "POST",
        dataType: 'json',
        async: false,
        data: { xmlString: xml },
        contentType: 'application/json; charset=utf-8',
        success: alert("File Saved in C:\\ Drive as Tasks.xml")
    });
}

Controller:

public ActionResult Save(string xmlString)
{
    XmlDocument doc = new XmlDocument();
    try
    {
        doc.LoadXml(xmlString);
    }
    catch(Exception e)
    {
        Console.WriteLine(e);
    }
    doc.Save(@"C:\\Tasks.xml");

    W6ViewModel viewModel = new W6ViewModel();
    viewModel.engineers = db.W6ENGINEERS.ToList();
    viewModel.tasks = db.W6TASKS.ToList();
    viewModel.skills = db.W6TASKS_REQUIRED_SKILLS1.ToList();
    viewModel.categories = db.W6TASKTYPECATEGORY.ToList();

    gatherInfo(viewModel);

    return View("Index", viewModel);
}

When trying to save three events (Dhtmlx objects) it works flawlessly, when trying to add more data to the XML I get this error (read form FireBug):

The length of the query string for this request exceeds the configured maxQueryStringLength value.

Any help would be greatly appreciated. Thanks!

Firebug Console: enter image description here

Stephen Sugumar
  • 545
  • 3
  • 9
  • 35
  • 1
    Are you using FireBug to simulate the POST? – DavidG Jul 21 '14 at 17:14
  • have you looked [here](http://stackoverflow.com/questions/8159321/request-exceeds-the-configured-maxquerystringlength-when-using-authorize) – Jonesopolis Jul 21 '14 at 17:15
  • 2
    In firebug - is json sent in body request? If yes, you should try to get content of body directly from request `Stream`, not via binder. –  Jul 21 '14 at 17:18
  • Sorry, I'm not to experienced with the terminology or FireBug for that matter. But yes, the content is located in the body section of the page. – Stephen Sugumar Jul 21 '14 at 17:24
  • 1
    What @pwas and I are essentially asking is: how is the data being sent to your controller? Your error suggests it is being sent as a query string instead of HTTP POST body content. – DavidG Jul 21 '14 at 17:25
  • In a network tab, you should be able to see the `POST` request that you are sending. Check if json object is stored in URL or after the request headers. –  Jul 21 '14 at 17:25
  • I believe the info is being posted in the URL, check my edit picture for reference. I hope this is what you guys meant. – Stephen Sugumar Jul 21 '14 at 17:35
  • 1
    That's the problem - you are making `GET` instead of `POST`. First of all, change `Type` to `type` in `$.ajax`. Secondly, add `HttpPost` attribute to your action. Without this, action will work only for `GET` requests. –  Jul 21 '14 at 17:40

3 Answers3

4

I believe jQuery.ajax uses type and not Type. This is case sensitive. If you look at your Firebug it is doing a GET request instead of a POST:

$.ajax({
    url: url,
    Type: "POST",
    dataType: 'json',
    async: false,
    data: { xmlString: xml },
    contentType: 'application/json; charset=utf-8',
    success: alert("File Saved in C:\\ Drive as Tasks.xml")
});

should be

$.ajax({
    url: url,
    type: "POST",
    dataType: 'json',
    async: false,
    data: { xmlString: xml },
    contentType: 'application/json; charset=utf-8',
    success: alert("File Saved in C:\\ Drive as Tasks.xml")
});
Dismissile
  • 32,564
  • 38
  • 174
  • 263
  • 2
    Very important: controller's action should be decorated with `HttpPost` attribute - otherwise will be visible only for `GET` requests. –  Jul 21 '14 at 17:42
  • @pwas If you don't decorate a controller, it can be either POST or GET (probably event DELETE, PUT, etc). If he had decorated it with `HttpPost` however, he would have gotten a 404 and the problem might have been easier to identify. – Dismissile Jul 21 '14 at 17:44
  • 1
    @Dismissile:I think default value is HttpGET – malkam Jul 21 '14 at 17:46
  • @malkam I just wrote a quick demo program to prove this and if you do not use any attribute on an action method in MVC, then it will accept GET or POST. – Dismissile Jul 21 '14 at 17:47
  • Hello, after making the apporpiate changes mentioned above, I now get this error: Invalid JSON primitive: xmlString. – Stephen Sugumar Jul 21 '14 at 17:54
  • 1
    @malkam There isn't really a default as such, but adding the attribute will restrict it to that method only. – DavidG Jul 21 '14 at 18:11
1

Grr I can't comment yet, I seem to recall if you pass one primitive value you do

data: xml 

rather than

data : { xmlString: xml }
-1

has explained in this post -> request exceeds the configured maxQueryStringLength when using [Authorize]

you should change the configuration setting in your web.config to allow more that the 2048 character of the default value.

so basicly you need to change the <httpRuntime maxRequestLength = "" maxQueryStringLength =""> to the number you find acceptable.

Edit: have you tried to change your Type: "POST" to type: "POST" ? since javascript is a case sensitive language.

As for the invalid Json primitive try this :

    $.ajax({
            url: url,
            type: "POST",
            dataType: 'json',
            async: false,
            data: 'xmlString='+ xml,
            contentType: 'application/json; charset=utf-8',
            success: alert("File Saved in C:\\ Drive as Tasks.xml")
        });
Rugdr
  • 199
  • 2
  • 15
  • This is probably a very bad idea. the code should be putting the data in the POST body, not in a query string. – DavidG Jul 21 '14 at 17:21
  • This does solve the problem, but I fear it is not the best way to go about sending large information to the server. Is there another way of sending data? – Stephen Sugumar Jul 21 '14 at 17:22
  • that is true, while the method i posted should get you running there is mostly a problem in the code elsewhere that take the post data and convert it to query string. – Rugdr Jul 21 '14 at 17:25
  • Yes, exactly, as of now, I don't know of a better way of handling this situation. – Stephen Sugumar Jul 21 '14 at 17:26
  • can we se your route configuration ? please – Rugdr Jul 21 '14 at 17:34