0

I'm currently working on an ASP.Net MVC project. I have a JavaScript function which takes an XML string as input. I would like to send this to the controller.

I have done so using an AJAX request, but in the controller the string is null.

View:

        function save() {
            var xml = scheduler.toXML();
            alert(xml);

            var url = '@Url.Action("Save", "Home")'

            $.ajax({
                url: url,
                Type: "POST",
                dataType: 'json',
                async: false,
                data: xml,
                contentType: 'application/json; charset=utf-8',
                success: function (data) { alert("OK");},
                error: function (jqXHR, exception) {
                alert('Error message.');
            }
            });

Controller:

 public ActionResult Save(string xml)
    {
        Console.WriteLine(xml);

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

        var engList = new List<object>();
        foreach (var engineer in viewModel.engineers)
        {
            engList.Add(new { key = engineer.ID, label = engineer.Name });
        }
        ViewBag.engineers = engList;

        return View("Index", viewModel);
    }

var xml = scheduler.toXML()

alert(xml):

enter image description here

Error Code (Sorry, wall of text):

[HttpRequestValidationException (0x80004005): A potentially dangerous Request.QueryString value was detected from the client (xmlString=&quot;&lt;data&gt;&lt;event&gt;
4
  • 1
    everything seems to be XML, but the contenttype is json ? Commented Jul 3, 2014 at 13:55
  • yes your right, but the scheduler.toXML() returns a sting I believe.docs.dhtmlx.com/scheduler/api__scheduler_toxml.html Commented Jul 3, 2014 at 14:04
  • there is also a dataType: "xml" which should set the contentType for you Commented Jul 3, 2014 at 14:04
  • I get an error when i change the dataType to XML. Commented Jul 3, 2014 at 14:11

2 Answers 2

4

Name your parameter like this:

function save() {
        var xml = scheduler.toXML();
        alert(xml);

        var url = '@Url.Action("Save", "Home")';

        $.ajax({
            url: url,
            Type: "POST",
            dataType: 'json',
            async: false,
            data: { xml: xml},
            contentType: 'application/json; charset=utf-8',
            success: function (data) { alert("OK");},
            error: function (jqXHR, exception) {
            alert('Error message.');
        }
        });

Also put this tag above you controller action:

[ValidateInput(false)]
Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately, when i do this, the error message kicks in, not sure why.
soory, but im not to sure what the error is, it just says error even after writing: error: function (jqXHR, exception) { alert(exception); }
Check second part of my answer
Is there a way to do this without using Ajax?
What do you want to achieve?
0

See the following ajax call:

  $.ajax({
            url: '@Url.Content("~/myaccount/CheckDuplicateEmailAddress")',
            data: { "emailAddress": email },
            async: false,
            type: "post",
            success: success,
            error: error
        });

And controller action is below. you need to send param as this:

data: { "emailAddress": email }

Remember case sensitivity and double quotes:

public bool CheckDuplicateEmailAddress(string emailAddress)
    {
    }

12 Comments

Tried this, i got an the same error as the answer mentioned above!
what does this alert gives. var xml = scheduler.toXML(); alert(xml);
It is just an alert saying "error" if there is a way to get the error, may you show me please?
when i do that, it is not null, but ill post it in a sec
yes you need to install a tool similar to firebug as in Mozilla. there you can see all javascript errors.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.