通过json将特殊字符POST到MVC

时间:2017-09-12 16:44:57

标签: json model-view-controller urlencode

我通过包含和&符号的json向控制器发送参数,字符串在该字符后被截断。一直在搜索,但找不到解决方案。

的JavaScript

var url = '/Common/DownloadFile?fileName=Me&You.xlsx';
$.ajax({
    type: 'POST',
    url: url,
    data: {},
    success: function (data) {}
});

控制器

public ActionResult DownloadFile(string fileName)
{
    // Here the param is coming in as 'Me'
    // Code removed for clarity
}

2 个答案:

答案 0 :(得分:2)

&符号用于分隔URL中的参数。如果您希望&符号成为参数值的一部分,则需要使用encodeURIComponent()等方法对其进行URL编码。

在这种特殊情况下,Me& You.xslx的编码版本将是Me%26You.xlsx。如果您在GET请求中指定该应用程序应该获得预期值。

答案 1 :(得分:2)

在网址和广告中需要转义,因为它用于添加新的参数,如

http://hello.com"?fileName=Something&otherFileName=SomethingElse

您需要通过百分比编码来转义&符号,如下所示:

/Common/DownloadFile?fileName=Me%26You.xlsx

以下是有关url参数转义escaping ampersand in url

的更多信息
相关问题