gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:32:58 25 4
gpt4 key购买 nike

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

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

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 Actions 允许我默认发送这些特殊字符。是真的吗?你如何解决这个问题?

$.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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com