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="<data><event>
2 Answers 2
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)]
-
Unfortunately, when i do this, the error message kicks in, not sure why.Stephen Sugumar– Stephen Sugumar2014年07月03日 14:05:33 +00:00Commented 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); }Stephen Sugumar– Stephen Sugumar2014年07月03日 14:09:43 +00:00Commented Jul 3, 2014 at 14:09
-
Check second part of my answerPaweł Reszka– Paweł Reszka2014年07月03日 14:47:32 +00:00Commented Jul 3, 2014 at 14:47
-
Is there a way to do this without using Ajax?HQSantos– HQSantos2020年01月31日 16:49:54 +00:00Commented Jan 31, 2020 at 16:49
-
What do you want to achieve?Paweł Reszka– Paweł Reszka2020年02月04日 11:06:59 +00:00Commented Feb 4, 2020 at 11:06
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)
{
}
-
Tried this, i got an the same error as the answer mentioned above!Stephen Sugumar– Stephen Sugumar2014年07月03日 14:07:33 +00:00Commented Jul 3, 2014 at 14:07
-
what does this alert gives. var xml = scheduler.toXML(); alert(xml);Learner– Learner2014年07月03日 14:11:45 +00:00Commented 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?Stephen Sugumar– Stephen Sugumar2014年07月03日 14:11:52 +00:00Commented Jul 3, 2014 at 14:11
-
when i do that, it is not null, but ill post it in a secStephen Sugumar– Stephen Sugumar2014年07月03日 14:12:21 +00:00Commented 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.Learner– Learner2014年07月03日 14:13:18 +00:00Commented Jul 3, 2014 at 14:13
Explore related questions
See similar questions with these tags.
XML
, but thecontenttype
isjson
?dataType: "xml"
which should set the contentType for you