简体   繁体   English

ASP.NET 内核 Ajax 返回 null

[英]ASP.NET Core Ajax returns null

function addTeam() {
    var teamName = $("teamField").val();
    $.ajax({
        type: "POST",
        url: 'AddTeam',
        contentType: "application/json;",
        data:
            { team: "HELLO PLEASE WORK" },
        success: function () {
            alert("URA");
        },
        error: function (error) {
            alert(error);
        }
    });
};

[HttpPost]
        public JsonResult AddTeam(string team)
        {
            teamRepository.Add(new Team { IsDelete = false, Name = team });
            teamRepository.SaveChanges(); 
            return Json(team);
        }

string team awlays returns null.字符串团队 awlays 返回 null。 i dont know whats the problem.我不知道是什么问题。 Searched for same issue in stackoverflow, but no one works for me在stackoverflow中搜索了相同的问题,但没有人适合我

It looks like you are sending in an object with the property "team", instead of just a raw string.看起来您正在发送带有属性“team”的 object,而不仅仅是一个原始字符串。

You can create a request object as your parameter:您可以创建一个请求 object 作为您的参数:

public class AddTeamRequest
{
    public string Team {get; set;}
}

And then use that as your parameter object:然后将其用作您的参数 object:

public JsonResult AddTeam(AddTeamRequest request)

You cant get value because you cant get it from html.You should get team name like this.您无法获得价值,因为您无法从 html 获得价值。您应该获得这样的团队名称。 Just add #.只需添加#。

 function addTeam() {
    var teamName = $("#teamField").val();///this line
    $.ajax({
        type: "POST",
        url: 'AddTeam',
        contentType: "application/json;",
        data:
            { team: "HELLO PLEASE WORK" },
        success: function () {
            alert("URA");
        },
        error: function (error) {
            alert(error);
        }
    });
};

You use application/json ,so you put the data into body,if you want to get the data in post method,you can use [FromBody] .And the data you pass need to use JSON.stringify(xxx) to change the data type before ajax .您使用application/json ,因此您将数据放入 body ,如果您想在 post 方法中获取数据,您可以使用[FromBody] 。您传递的数据需要使用JSON.stringify(xxx)来更改数据在ajax之前输入。 Here is a demo worked:这是一个演示:

View:看法:

var team1 = "HELLO PLEASE WORK";
        var team = JSON.stringify(team1) ;
        $.ajax({
            type: "POST",
            url: 'AddTeam',
            contentType: "application/json;charset=utf-8",
            data: team,
            success: function () {
                alert("URA");
            },
            error: function (error) {
                alert(error);
            }
        });

Controller: Controller:

[HttpPost]
        public JsonResult AddTeam([FromBody]string team)
        {
            
            return Json(team);
        }

result:结果: 在此处输入图像描述

Or you can use contentType: "application/x-www-form-urlencoded", ,so that you don't need to use [FromBody] and don't need to change the data type.或者你可以使用contentType: "application/x-www-form-urlencoded", ,这样你就不需要使用[FromBody]也不需要改变数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM