【问题标题】:Can`t send array using ajax with asp.net core无法使用带有 asp.net 核心的 ajax 发送数组
【发布时间】:2018-07-09 10:31:39
【问题描述】:

我已经写了这个 ajax 函数:

$(function () {
        $("#showReport").click(function () {
            var data = [];
            for (var i = 0; i <@Model.LiFilters.Count;i++) {
                data[i] = $("#filter" + i).val();
                $("#myDiv").html("Hello!");
            }

            alert('{filters:' + JSON.stringify(data) + '}');
            $("#myDiv").remove();
            $.ajax({
                type: "POST",
                url: '@Url.Action("ShowReport", "Report")',
                traditional: true,
                //data: '{filters:' + JSON.stringify(data) + '}',
                data: { filters : data },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    alert(data);
                    alert(response[0]);
                    $("#myDiv").html(response[0]);
                    alert(response);
                },
                failure: function (response) {
                    alert(response.responseText);
                },
                error: function (response) {
                    alert(response.responseText);
                }
            });
            return false;
        });
    });

并在报表控制器中执行此操作:

 [HttpPost]
public JsonResult ShowReport(string[] filters)
{
    C_AminReport aminReport = null;
    string sErr = string.Empty;
    //C_ReportParams aminParams = null;
    //C_AminReport aminReport;
    string sReport = "Empty Report!";
    if (C_AminReport.U_GetReport(aminReport.ID, out aminReport))
    {
        int iConIndex = Get_ServiceIndex();
        aminReport.U_RenderReport(iConIndex, ref sErr);
        aminReport.U_GetReportFromAmin(iConIndex, aminReport, out sReport, ref sErr);
    }
    string[] asReport;
    JsonBuilder.U_TryReadJson(sReport, out asReport);
    return Json(asReport);
}

但当运行代码时,我看到数据数组已填充但在 ShowReport 操作内部,填充器数组是一个空数组且未填充! 我还在动作参数中尝试了 [FromBody] 标记,但它不起作用! 有什么问题?

【问题讨论】:

  • 打开浏览器的开发者工具,看看请求是如何被格式化的。这个 JSON 是什么样子的?
  • @GabrielLuci 看起来像这样:{filters:["1","2","3","4","5","6","7"]}
  • 试着把[FromBody]放在string[] filters前面
  • @GabrielLuci 将其更改为“public JsonResult ShowReport([FromBody]string[] filters)”时,填充器设置为 null insted of string[0]!

标签: jquery asp.net arrays ajax asp.net-core


【解决方案1】:

我测试了您的 JavaScript,它正在尝试发送查询字符串中的所有数据。

你必须使用JSON.stringify。其中之一应该可以工作:

data: '{filters:' + JSON.stringify(data) + '}'

data: JSON.stringify(data)

我怀疑这是第二个可行的方法。

你可能还需要[FromBody]

【讨论】:

    【解决方案2】:

    您发送的不是数组,而是一个具有“过滤器”属性的对象,它是一个数组。

    {filters:["1","2","3","4","5","6","7"]}
    

    正如你在评论中指出的:)

    尝试直接发送数组:

    $.ajax({
       type: "POST",
       url: '@Url.Action("ShowReport", "Report")',
       traditional: true,
       //data: '{filters:' + JSON.stringify(data) + '}',
       data: data, // <=== !!
       contentType: "application/json; charset=utf-8",
    

    添加属性 [FromBody] 也不错:-)

    【讨论】:

    • 我的代码中没有注释这一行!不知何故,我尝试了你的方法并使用数据:数据和 [FromBody] 但它也不起作用:(
    • 我的意思是您帖子下的评论 ;-) 无论如何,您是否从失败或错误处理程序中得到任何响应或任何消息?
    • 没有失败消息,只是在请求正文中发送如下内容: undefined=&undefined=&undefined=&undefined=&undefined=&undefined=&undefined=
    • 如果您可能需要,这是我的请求标头: 主机:localhost:51519 用户代理:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0 接受: 应用程序/json, 文本/javascript, /; q=0.01 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Referer: localhost:51519/Report/ShowReport/13 Content-Type: application/json; charset=utf-8 X-Requested-With: XMLHttpRequest Content-Length: 76 Cookie: _ga=GA1.1.949841155.1515651415; .AspNetCore.Session=somecode Connection: keep-alive
    • 试试数据:JSON.stringify(data),
    【解决方案3】:

    我在健身房用手机打字,很抱歉出现拼写错误和语法错误。我通常喜欢强输入控制器的输入参数。因此,您可以使用FilterList filters 而不是string [] filters,其中FilterList 被定义为例如

    public FilterList { public List<string> filters {get; set;} }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-08
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 2015-10-11
      相关资源
      最近更新 更多