我有一个需要 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/