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

Matheus Cuba
  • 2,068
  • 1
  • 20
  • 31
  • 1
    Do not build JSON like that. If a user enters `'` in the text field, they have immediately broken it. – ProgrammingLlama Dec 29 '17 at 09:11
  • then how can i save my value because it's show me only null value –  Dec 29 '17 at 09: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](https://stackoverflow.com/questions/40853188/frombody-string-parameter-is-giving-null). – ProgrammingLlama Dec 29 '17 at 09:14
  • 2
    As a side note, `async: false` is deprecated and should never be used – A. Wolff Dec 29 '17 at 09:15

2 Answers2

1

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);
}
lucky
  • 12,734
  • 4
  • 24
  • 46
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].
naveen
  • 53,448
  • 46
  • 161
  • 251