【问题标题】:Why is my JSON string null on the server side when sent using www-form-urlencoded?为什么我的 JSON 字符串在使用 www-form-urlencoded 发送时在服务器端为空?
【发布时间】:2023-03-26 16:02:02
【问题描述】:

我有一个使用 jquery / ajax 的 ASP.NET / MVC4 应用程序。

我正在尝试使用 $.ajax( ... ) 将一个非常大的字符串从客户端发送到服务器

首先,我们使用 contentType "application/json" 来完美运行。然而,在这种特殊情况下,服务器会抛出异常,因为正在传输的数据太长。我已经尝试了所有方法来增加 web.config 文件中反序列化程序的 maxJsonLength,但它不起作用,没有人能弄清楚原因。

有人建议,作为一种变通方法,将 contentType 发送为 "application/x-www-form-urlencoded; charset=UTF-8",然后让我的控制器手动反序列化对象,而不是让 MVC 框架去做。

Javascript:

function AjaxRequest(data, dataType, type, url, contentType, success, error, args)
{
    if (url.indexOf("MyController/SomeMethodInMyController") > 0)
        contentType = "application/x-www-form-urlencoded; charset=UTF-8";

    data = JSON.stringify(data);

    $.ajax({async: args.async, cache: args.cache, data: data, dataType: dataType, type: type, url: url, contentType: contentType, traditional: true, headers : { ... }, beforeSend: { ... }, success: { ... }, error: { ... }, complete: { ...});
}

function SomeFunction()
{
    var object = {};
    object.Name = $("#someControl").val();
    object.RtfData = $("someOtherControl").val();

    AjaxRequest(object, 'html', 'post', 'MyController/SomeMethodInMyController', 'application/json;', function (response) { ... }, function (error) { ... });
}

在这种情况下,我的应用程序不再在 MVC 框架尝试自行反序列化对象的“内部”崩溃。现在它绕过了所有这些,直接调用了我的控制器中的方法。有点老套,但 3 天后我会尽我所能。

C#:

public void SomeMethodInMyController(string formData)
{
    JavaScriptSerializer jss = new JavaScriptSerializer();
    jss.MaxJsonLenght = int.MaxValue;

    MyType objMyType = jss.Deserialize<MyType>(formData);

    //do stuff with objMyType
}

问题是当我在这个方法中设置断点时,formDatanull

在我的浏览器控制台中,在实际执行$.ajax(); 之前,我将typeof(data) 输入到返回"string" 的控制台中。如果我将鼠标悬停在符号上,我可以看到我希望它包含的所有数据都在那里。那么为什么在我的 C# 代码中值是null

【问题讨论】:

    标签: c# asp.net json ajax


    【解决方案1】:

    我认为您需要发送FormData object 而不仅仅是字符串。尝试像这样更改您的 AjaxRequest 函数:

    function AjaxRequest(data, dataType, type, url, contentType, success, error, args)
    {
        if (url.indexOf("MyController/SomeMethodInMyController") > 0)
            contentType = "application/x-www-form-urlencoded; charset=UTF-8";
    
        var form = new FormData();
        form.append("MyData", JSON.stringify(data));
    
    
        $.ajax({processData: false, async: args.async, cache: args.cache, data: form, dataType: dataType, type: type, url: url, contentType: contentType, traditional: true, headers : { ... }, beforeSend: { ... }, success: { ... }, error: { ... }, complete: { ...});
    }
    

    【讨论】:

    • 这会导致 javascript 异常“非法执行”。
    • 你的意思是“非法调用”吗?我更新了答案以将“processData:false”添加到 ajax 设置 - 你能看看你是否仍然得到异常吗?
    • 啊,是的,我的错。现在查看错误日志:TypeError - Java Script Error : Illegal invocation: at document path 'localhost/myApp'.&amp;#xA; at AjaxRequest('[object Object]', 'html', 'post', '/MyController/SomeMethodInMyController', 'application/json;', 'function (response) {&amp;#xD;&amp;#xA; //if the response is not null we are showing the pop up to download the file.&amp;#xD;&amp;#xA; 然后只给出堆栈跟踪的其余部分。
    • 我刚刚看到你的编辑;我现在就试试……测试这个 bug 大约需要 10 分钟,所以我知道这需要一点时间……
    • 也尝试添加这两个设置,以防万一:contentType: false, mimeType: "multipart/form-data"
    猜你喜欢
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多