【问题标题】:Getting a JavaScript object into my controller: What am I missing here?将 JavaScript 对象放入我的控制器:我在这里缺少什么?
【发布时间】:2015-06-15 22:15:15
【问题描述】:

当我尝试运行时收到500 (Internal Server Error)

    $.ajax({
        xhr: function() {
            var xhr = new window.XMLHttpRequest();
            return xhr;
        },
        type: 'POST',
        url: '@Url.Action("MyAction","MyController")',
        data:  fi,
        contentType: "application/json; charset=utf-8",
        dataType: "json", // dataType is json format
        success: function(retJson) {
            // ...

        }
    });

其中fi 是像{org: "string1", cat: "string2", fileName: "string3"} 这样的JavaScript 映射,而myAction 就像

    [HttpPost]
    public async Task<JsonResult> myAction (FileInfo fi)
    {

FileInfo

定义
    public class FileInfo
    {
        public string org { get; set; }
        public string cat { get; set; }
        public string fileName { get; set; }
    }

知道我在这里缺少什么吗?

【问题讨论】:

  • 什么是 500 服务器错误?
  • 500 响应的堆栈跟踪中的内容是什么,应该提供更多详细信息?
  • JavaScript 控制台打印错误。
  • 删除contentType: "application/json; charset=utf-8",(或者使用data: JSON.stringify(fi),

标签: c# asp.net ajax json asp.net-mvc


【解决方案1】:

问题在于 JSON 对象的定义方式。

它应该有这样的道具名称的单引号:

 {'org': 'string1', 'cat': 'string2', 'fileName': 'string3'}

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 2020-09-21
    • 2010-12-24
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多