Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX & Web Api Post Method - How does it work?

Tags:

I am trying to write to my database using AJAX / Jquery and c#. Whenever I pass the parameter in to the C# code it shows as null. I am using the default template that visual studio generates when creating a controller class. Any help would be appreciated!

NOte: This is a rest service that I am trying to call. (A regular ASP website... not MVC. Also, the GET Rest api works perfectly.)

Jquery/AJAX:

var dataJSON = { "name": "test" }  $('#testPostMethod').bind("click", GeneralPost); function GeneralPost() {     $.ajax({         type: 'POST',         url: '../api/NewRecipe',         data:JSON.stringify(dataJSON),         contentType: 'application/json; charset=utf-8',         dataType: 'json'     }); } 

C#

    //If I remove the [FromBody] Tag then when I click the button this method is never called.     public void Post([FromBody]string name)      {      } 

EDIT:

I have adjusted my code slightly but am still encountering the same issue. To recap, It is loading the POST method, but it is passing in null.

C#

 public class RecipeInformation     {         public string name { get; set; }      }          public void Post(RecipeInformation information)          {          } 

AJAX:

    var dataJSON = { information: { name: "test" } };      $('#testPostMethod').bind("click", GeneralPost);     console.log(dataJSON);     function GeneralPost() {         $.ajax({             type: 'POST',             url: '../api/NewRecipe',             data: dataJSON,             contentType: 'application/json; charset=utf-8',         });     } 
like image 691
Yecats Avatar asked Mar 28 '13 17:03

Yecats


People also ask

Why is Ajax named Ajax?

So why the name Ajax? Founded on March 18, 1900 by Floris Stempel, Carel Reeser and Han Dade they decided to name their new club after the mythological hero Ajax, who fought in the Trojan War.

What does Ajax mean in Amsterdam?

The club was founded in Amsterdam on 18 March 1900 by Floris Stempel, Carel Reeser and Han Dade, the second incarnation after a short-lived previous attempt—as Football Club Ajax—in 1894. The club was named after the mythological hero Ajax, a Greek who fought in the Trojan War against Troy.

How much money does AFC Ajax have?

#Ajax have a sustainable business model, reporting profits in 9 of the last 10 years (only loss was €1m in 2015/16). In that period, they have accumulated over a quarter of a billion Euros profit, averaging €26m a season. However, they do expect a loss in 2020/21 due to COVID.


Video Answer


2 Answers

For simple type, on server side:

public void Post([FromBody]string name) { } 

on the client side, you just define if you want to send in json format:

    var dataJSON = "test";      $('#testPostMethod').bind("click", GeneralPost);     function GeneralPost() {         $.ajax({             type: 'POST',             url: '/api/NewRecipe',             data: JSON.stringify(dataJSON),             contentType: 'application/json; charset=utf-8',             dataType: 'json'         });     } 

If you want to make it work in complex type, from server side you should define:

public class RecipeInformation {     public string name { get; set; } }  public class ValuesController : ApiController {     public void Post(RecipeInformation information)     {     } } 

And from client side:

    var dataJSON = { name: "test" };      $('#testPostMethod').bind("click", GeneralPost);     function GeneralPost() {         $.ajax({             type: 'POST',             url: '/api/NewRecipe',             data: JSON.stringify(dataJSON),             contentType: 'application/json; charset=utf-8',             dataType: 'json'         });     } 
like image 70
cuongle Avatar answered Nov 02 '22 06:11

cuongle


You can try doing something like this and use the jquery param method

    var postData = {         name : 'name'     }      $('#testPostMethod').bind("click", GeneralPost);     function GeneralPost() {         $.ajax({             type: 'POST',             url: '../api/NewRecipe',             data: $.param(postData,true),             contentType: 'application/json; charset=utf-8',             dataType: 'json'         });     } 
like image 44
itsstephyoutrick Avatar answered Nov 02 '22 05:11

itsstephyoutrick