This is my api code that return successfull json data while using get method
public Question[] Get() {
getQuestion obj = new AllDataAccess.getQuestion();
return obj.questionList().ToArray();
}
This is my post method data that accept the value and save in database
public void Post([FromBody] string question) {
SaveQuestion obj = new AllDataAccess.controller.SaveQuestion();
obj.savaData(question);
}
This is the method that call my api
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'http://localhost:53893/api/values',
data: "{'question':'" + $("#submit").value + "'}",
dataType: 'json',
async: false,
success: function(data, status) {
console.log(status);
},
error: function(err) {
console.log(err);
}
});
Now the problem is when i post the data with one textbox value its give me a message in console that "nocontent" and record save in data base with null value
If you want to use
FromBody
, you can do so. JavaScriptAPI action
You had these issues.
content-type
for your ajax call.val()
should be used instead of.value
.[HttpPost]
.It seems that your ajax url is wrong. You should specify the action name
(post)
. Also, useJSON.stringify
to retrieve proper json from javascript object.In the server side, you should create a model class for
Post
method;And then
Post
method looks like;