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;
gariepy
3,6846 gold badges23 silver badges34 bronze badges
asked Jul 3, 2014 at 13:53
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)]
answered Jul 3, 2014 at 13:55
5
  • Unfortunately, when i do this, the error message kicks in, not sure why. Commented Jul 3, 2014 at 14:05
  • soory, but im not to sure what the error is, it just says error even after writing: error: function (jqXHR, exception) { alert(exception); } Commented Jul 3, 2014 at 14:09
  • Check second part of my answer Commented Jul 3, 2014 at 14:47
  • Is there a way to do this without using Ajax? Commented Jan 31, 2020 at 16:49
  • What do you want to achieve? Commented Feb 4, 2020 at 11:06
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)
 {
 }
HQSantos
7641 gold badge7 silver badges16 bronze badges
answered Jul 3, 2014 at 14:04
12
  • Tried this, i got an the same error as the answer mentioned above! Commented Jul 3, 2014 at 14:07
  • what does this alert gives. var xml = scheduler.toXML(); alert(xml); Commented Jul 3, 2014 at 14:11
  • It is just an alert saying "error" if there is a way to get the error, may you show me please? Commented Jul 3, 2014 at 14:11
  • when i do that, it is not null, but ill post it in a sec Commented Jul 3, 2014 at 14:12
  • yes you need to install a tool similar to firebug as in Mozilla. there you can see all javascript errors. Commented Jul 3, 2014 at 14:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.