I wrote simple form, which have simple form with button. Onclick with javascript method on this button should fire a ajax which should go to '/Parser/GetParseData'. But I always got error without any detail (always run request.fail).
I don't know what is problem. General My aim is fire ajax with data from form and after success (and getting answer) I want to put received string in $('#result').
Could you help me?
Ok my View:
@using Project.Domain.Entities
@model ParserHelper
@{
ViewBag.Title = "Project";
}
<h2>simple form asp.net mvc 5</h2><br/>
@using (Html.BeginForm())
{
<fieldset>
<div class="editor-field">
@Html.TextAreaFor(Model => Model.InputData)
</div>
<div class="radio-field">
@Html.RadioButtonFor(Model => Model.TypeOfResult, "xml") @Html.Label("Xml") <br/>
@Html.RadioButtonFor(Model => Model.TypeOfResult, "csv") @Html.Label("Csv")
</div>
<button onclick="submitTest()">Request!</button>
</fieldset>
}
<div id="result">
</div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
function submitAlert() {
alert("testtest");
}
function submitTest() {
event.preventDefault();
var data = {
InputData: $('#InputData').val(),
TypeOfResult: $('#TypeOfResult').val()
};
request = $.ajax({
url: "/Parser/GetParseData",
type: "post",
formData: data
});
request.done(function (response, textStatus, jqXHR) {
alert("Hooray, it worked!");
$('#result').text('Its worked:' + response);
});
request.fail(function (jqXHR, textStatus, errorThrown) {
alert(
"The following error occurred: " +
textStatus, errorThrown
);
$('#result').text('Sorry, error');
});
}
</script>
My model is:
public class ParserHelper : IFormHelper
{
public string ResultAfterParse { get; set; }
public string InputData { get; set; }
public string TypeOfResult { get; set; }
}
Finally here is my controller metho in: ParserController : Controller:
[HttpPost]
public string GetParseData(IFormHelper formData)
{
return "ANSWER!!!";
}
I also added this as Routing:
routes.MapRoute(
name: "PostData",
url: "{controller}/{action}",
defaults: new { controller = "Parser", action = "GetParseData"
});
If you need any more information let me know - I will edit this post.
SOLVED.