AJAX和Web Api发布方法-如何工作?

AJAX和Web Api发布方法-如何工作?,第1张

AJAX和Web Api发布方法-如何工作?

对于简单类型,在服务器端

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

客户端,您只需定义是否要以json格式发送:

    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'        });    }

如果要使其以复杂类型工作,则应从服务器端定义:

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

从客户端:

    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'        });    }


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5008260.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-15

发表评论

登录后才能评论

评论列表(0条)

    保存