weixin_33681778 2018-08-13 15:57 采纳率: 0%
浏览 54

ajax POST发送null

I originally wrote the call as a GET but found a limitation with the length of the URI. Ideally, the call will take an object and turns it into a JSON format string, then sends it to a controller which will encrypt that string. The controller will send back a true/false if it succeeded.

My problem with POST, once it reaches the controller, the data parameter set from the ajax is null.

Here is the ajax/js:

var encActionURL = '@Url.Action("Encrypt", "Home")';
$.ajax({
    url: encActionURL,
    type: "POST",
    contentType: "application/json; charset=utf-8;",
    dataType: "json",
    async: true,
    traditional: true,
    data: { jsonDoc: JSON.stringify(jsonDataFile) },
    success: /*OnSuccess*/ function (result) {
        // DO STUFF;
    }
});

Here is the controller:

[HttpPost]
public bool Encrypt(string jsonDoc)
{
    return serverConnection.Encrypt();
}

Note that, when I simply change the type to 'GET', it works great but when the form gets too long, it throws a 414 status error.

Most of the fixes found that I seem to have is the 'application/json'. I've also set ajax to traditional.

  • 写回答

1条回答 默认 最新

  • weixin_33736048 2018-08-13 19:17
    关注

    After going through a rabbit-hole of security tokens and validating forms, it wasn't any of that... this might be a solution for anyone using ASP.NET Core 2.1 MVC (5?) or just in general. Could have been a syntax mistake, return type mistake, or a combination.

    New Ajax

    $.ajax({
        url: encActionURL,
        type: "POST",
        data: { 'jsonDoc': JSON.stringify(jsonDataFile) }, // NOTICE the single quotes on jsonDoc
        cache: false, 
        success: /*OnSuccess*/ function (result) {
            // DO STUFF;
        }
    });
    

    New Controller

        [HttpPost]
        public ActionResult EncryptJSON(string jsonDoc) // Switch to ActionResult, formerly JsonResult
        {
            return Json(serverConnection.Encrypt());
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配