I have a problem with a form in my Web API. The form is very simple. The user enters a string, the controller receives the string and inserts it into a database with an id. The connection with the database works and I can read from it safely.
EDIT This is the code in the controller. EDIT 2 This is the correct and working controler
public ActionResult PostMyData(string json)
{
try
{
var newEntry = new Questions() { Id = json.Id, Question= json.Question};
context.Questions.Add(newEntry);
context.SaveChanges();
return Ok();
}
catch (Exception e)
{
return BadRequest();
}
}
This is a part of the HTML page. EDIT This is the updated script that does not return errors. EDIT 2 This is the correct and working script
<script language="Javascript">
$(document).on('click', '#submitButt', function () {
var myquestion = $('#question').val();
var json = {
Id : 1,
Question: myquestion
}
$.ajax({
type: "POST",
url: "api/Simple",
data: JSON.stringify(json),
dataType: "json",
contentType:"application/json",
success: function (data) {
alert(data);
},
error: function (data) {
alert("An Issue has occured");
}
});
})
</script>