dimanche 17 septembre 2017

lightest dependency causing AJAX (post method) data type

I have this Jquery code in front end:

        var person = { Name: name, Email: email, Phone: phone };

        $.ajax({
            type: "POST",
            url: serviceURL,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(person) ,
            traditional : true,
            dataType: "json",
            success: successFunc,
            error: errorFunc
        });

which calls the following ASP.Net MVC controller:

    [HttpPost]
    public JsonResult Register(Person person)
    {
       // Do something with person 
    }

Person class is :

  public class Person 
  {
       public string Name { get; set; }
       public string Email { get; set; }
       public string Phone { get; set; }
  }

so my question is what is the less dependency causing AJAX post data type ? like if I used person class formater of my controler , if after 2 month I desired to add new fildes I will have to edit the class , I will depend on the class. so using application/x-www-form-urlencoded; charset=UTF-8 datatype might be better and less dependency causing ? since I will deal only with key value pair ? what is the best strategy to have most flexible code or even if some approach is better than the other in another way. as I'm willing to become more mature and write code that will cause less problems in the future.

Aucun commentaire:

Enregistrer un commentaire