AJAX和放大器;网络阿比POST方法 - 它是如何工作的?它是、放大器、阿比、方法

2023-09-10 16:02:34 作者:怀念微笑小姐

我想写使用AJAX / jQuery和C#我的数据库。每当我传递参数到C#code它显示为空。我使用的是创建一个控制器类时,Visual Studio生成的默认模板。任何帮助将是AP preciated!

请注意:这是我试图调用REST服务。 (定期的ASP网页...不是MVC,此外,GET REST API完美的作品。)

jQuery的/ AJAX:

  VAR dataJSON = {名:测试}

        $('#testPostMethod')绑定(点击,GeneralPost);
        传播GeneralPost(){
            $阿贾克斯({
                键入:POST,
                网址:../api/NewRecipe,
                数据:JSON.stringify(dataJSON)
                的contentType:应用/ JSON的;字符集= UTF-8,
                数据类型:JSON
            });
        }
 

C#

  //如果我删除[FromBody]标签然后当我按一下按钮该方法不会被调用。
    公共无效后([FromBody]字符串名称)

    {

    }
 
放大器的作用 放大器工作原理 放大器的分类

编辑:

我还稍微调整了code,但我仍然遇到了同样的问题。总括来说,它加载的是POST方法,但它传递空。

C#

 公共类RecipeInformation
    {
        公共字符串名称{;组; }

    }

        公共无效后(RecipeInformation信息)

        {

        }
 

AJAX:

  VAR dataJSON = {信息:{名称:测试}};

    $('#testPostMethod')绑定(点击,GeneralPost);
    执行console.log(dataJSON);
    传播GeneralPost(){
        $阿贾克斯({
            键入:POST,
            网址:../api/NewRecipe,
            数据:dataJSON,
            的contentType:应用/ JSON的;字符集= UTF-8,
        });
    }
 

解决方案

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

 公共无效后([FromBody]字符串名称)
{
}
 

在客户端,你只是定义,如果你想发送JSON格式的:

  VAR dataJSON =测试;

    $('#testPostMethod')绑定(点击,GeneralPost);
    传播GeneralPost(){
        $阿贾克斯({
            键入:POST,
            网址:/ API / NewRecipe',
            数据:JSON.stringify(dataJSON)
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据类型:JSON
        });
    }
 

如果你想使其在复杂类型的工作,从服务器端,您应该定义:

 公共类RecipeInformation
{
    公共字符串名称{;组; }
}

公共类Values​​Controller:ApiController
{
    公共无效后(RecipeInformation信息)
    {
    }
}
 

和从客户端:

  VAR dataJSON = {名称:测试};

    $('#testPostMethod')绑定(点击,GeneralPost);
    传播GeneralPost(){
        $阿贾克斯({
            键入:POST,
            网址:/ API / NewRecipe',
            数据:JSON.stringify(dataJSON)
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据类型:JSON
        });
    }
 

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

解决方案

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