jquery Ajax Post在Asp.net Core in .Net 6中无法在Action方法中工作

n3ipq98p  于 2023-10-17  发布在  jQuery
关注(0)|答案(4)|浏览(111)

我在cshtml中做了一个类似下面的调用:

$(document).ready(function(){
    $('.dl-dir-list').click(function(e){
        console.log($(e.target).data('path'));
        console.log(JSON.stringify({path: $(e.target).data('path')}));
        $.ajax({
            type: "POST",
            url: '@Url.Action("GetFiles")',
            data: JSON.stringify({path: $(e.target).data('path')}),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (response) {
                console.log(response);
            },
            error: function () {
                alert("Error while getting files");
            }
        });
    });
});

行动方式:

[HttpPost]
        public JsonResult GetFiles([FromBody]string path)
        {
            return Json(_fileService.GetFilesFromDirectory(path));
        }

问题是路径参数始终为空。会是什么问题呢?这是在ASP.NET核心,.NET 6版本

dfuffjeb

dfuffjeb1#

1.确保$(e.target).data('path')包含值。
2.将您的代码更改为:

data: JSON.stringify($(e.target).data('path')),

整个工作演示:
检视:

//be sure add this `data-path` attribute
<input type="button" value="Create" class="dl-dir-list" data-path="aaa"/>

@section Scripts
{
    <script>
        $(document).ready(function(){
            $('.dl-dir-list').click(function(e){
                console.log($(e.target).data('path'))   //result: "aaa"
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("GetFiles")',
                    data: JSON.stringify($(e.target).data('path')), //change here....
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (response) {
                        console.log(response);
                    },
                    error: function () {
                        alert("Error while getting files");
                    }
                });
            });
        });
    </script>
}

控制器:

[HttpPost]
public JsonResult GetFiles([FromBody]string path)
{
    return Json(_fileService.GetFilesFromDirectory(path));
}
shstlldc

shstlldc2#

像下面这样试试,

data: JSON.stringify({"path": $(e.target).data('path')}),
6yoyoihd

6yoyoihd3#

要将JSON数据发送到ASP.NET Core 6控制器操作,需要进行一些更改。在这里,我创建了一个示例,其中包含所需的所有更改,并通过AJAX发送测试JSON数据
1.您的控制器操作应该如下所示

[HttpPost]
[Consumes("application/json")]
public IActionResult PostDataTest([FromBody] Employee[] employees)
{
    return Json(employees);
}
  1. Ajax方法看起来类似于
var jsonData = [{"id":0,"name":"Very Long Employee Name with Many Characters 0","doj":"2022-08-21T11:23:24.1220131+05:30"},{"id":1,"name":"Very Long Employee Name with Many Characters 1","doj":"2022-08-20T11:23:24.1236139+05:30"},{"id":2,"name":"Very Long Employee Name with Many Characters 2","doj":"2022-08-19T11:23:24.1236164+05:30"},{"id":3,"name":"Very Long Employee Name with Many Characters 3","doj":"2022-08-18T11:23:24.1236167+05:30"},{"id":4,"name":"Very Long Employee Name with Many Characters 4","doj":"2022-08-17T11:23:24.123617+05:30"}];

var strJson = JSON.stringify(jsonData);

$.ajax({
        url: "/home/PostDataTest",
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: strJson,
        success: function (result) {
            $("#txtJsonReturned").val(result);
        },
        error: function (err) {
            $("#txtJsonReturned").val("Error while uploading data: \n\n" + err);
        }
    });

我已经创建了一个示例来处理这个场景,并将其发布在GitHub https://github.com/rajabb/MvcCore6JsonUploadSample上。我希望这能解决问题。

7gcisfzg

7gcisfzg4#

你好,你从Ajax方法中删除这一行,并尝试它。内容类型:“application/json;字符集=utf-8”,
.Net核心不接受在一个方法中的“contentType”字段。

相关问题