javascript - ASP.net MVC - 操作不喜欢 JSON 字符串 400 错误请求

标签 javascript jquery asp.net-mvc

我有一个需要 2 个字符串的操作。其中一个字符串是一个又大又丑的 json 字符串。我怀疑该操作不允许传递特殊字符,因为我不断收到 400 - 错误请求。

可以将序列化的 json 对象传递给操作吗?

public ActionResult SaveState(string file, string state)
    {
        string filePath = GetDpFilePath(file);
        HtmlDocument htmlDocument = new HtmlDocument();
        htmlDocument.Load(filePath);
        HtmlNode stateScriptNode =
            htmlDocument.DocumentNode.SelectSingleNode("/html/head/script[@id ='applicationState']");
        stateScriptNode.InnerHtml = "var applicationStateJSON =" + state;
        htmlDocument.Save(filePath);

        return null;


    }

客户端脚本

  'e' is a large json string  

$.post('/State/SaveState/' + fileName+'/' + '/' + e + '/');

强文字

我现在使用 UriEncoding() 对文本进行编码,但这没有什么区别。我认为 MVC 操作默认不允许我发送这些特殊字符..是这样吗?您如何解决这个问题?

$.post('/State/SaveState/'+encodeURIComponent(fileName)+'/'+'/'+encodeURIComponent(e)+'/');

示例请求:

请求网址:http://localhost:51825/State/SaveState/aa6282.html//%7B%22uid%22%3A%22testUser%22%2C%22a

最佳答案

您需要在发出请求时对其进行编码:

$.post('/State/SaveState/' + encodeURIComponent(fileName) + '/' + encodeURIComponent(e));

关于javascript - ASP.net MVC - 操作不喜欢 JSON 字符串 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6012868/

相关文章:

javascript - 如何限制用户在输入字段中输入 24 格式时间?

asp.net-mvc - 应用服务层作为静态类

javascript - 程序运行后如何修复 undefined variable

javascript - 谷歌地图无法在类型之间进行更改

jquery - 更改滚动条的顶部边距

jQuery 父兄弟选择器

c# - IOwinContext 上下文为空

asp.net-mvc - 将我的自定义模型绑定(bind)到我的 POST Controller

javascript - Vue.js 在添加新行时尝试增加数组值

javascript - js如果数组中的任何值小于或大于值