0

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

4
  • 2
    Do not build JSON like that. If a user enters ' in the text field, they have immediately broken it. Commented Dec 29, 2017 at 9:11
  • then how can i save my value because it's show me only null value Commented Dec 29, 2017 at 9:12
  • On the JS side you should use JSON.stringify(model) and on the server side, you need to define a model rather than using string with FromBody. Commented Dec 29, 2017 at 9:14
  • 3
    As a side note, async: false is deprecated and should never be used Commented Dec 29, 2017 at 9:15

2 Answers 2

2

It seems that your ajax url is wrong. You should specify the action name (post). Also, use JSON.stringify to retrieve proper json from javascript object.

    var postData = { question:$("#submit").val() };
    $.ajax({
       type: 'POST',
       contentType: "application/json; charset=utf-8",
       url: 'http://localhost:53893/api/values/post',
       data: JSON.stringify(postData),
       dataType: 'json',
       success: function (data,status) {
           console.log(status);
       },
       error: function (err) {
           console.log(err);
       }
    });

In the server side, you should create a model class for Post method;

public class PostInput
{
    public string Question { get; set; }
}

And then Post method looks like;

[HttpPost]
public void Post([FromBody]PostInput input)
{
     SaveQuestion obj = new AllDataAccess.controller.SaveQuestion();
     obj.savaData(question);
}
Sign up to request clarification or add additional context in comments.

11 Comments

And he's also trying to use [FromBody] string rather than using a model.
@vaibhav Perhaps you're missing the [HttpPost] method on the model?
@vaibhav, as john said, you should add the [HttpPost] attribute on the method.
$("#submit").value is probably undefined btw... I guess OP wants $("#submit").val()
i correct my value mistake to val and i also define [httpost] but i still get error of 405
|
0

If you want to use FromBody, you can do so. JavaScript

$.ajax({
    type: "POST",
    //default content-type, could be omitted
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
    url: 'http://localhost:53893/api/values/post',
    data: {'': $("#submit").val()}
});

API action

[HttpPost]
public void Post([FromBody]string question)
{
     SaveQuestion obj = new AllDataAccess.controller.SaveQuestion();
     obj.savaData(question);
}

You had these issues.

  1. Wrong content-type for your ajax call.
  2. Data was not posted correctly.
  3. val() should be used instead of .value.
  4. API action should be decorated with [HttpPost].

1 Comment

@Rainman: stackoverflow.com/a/12072419/17447 this is what I understand too

Your Answer

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