asp.net-mvc - 与 Ajax.ActionLink 相比,$.ajax 调用中的请求 URL 编码问题以及 $.ajax 中的最大请求 URL 长度

标签 asp.net-mvc jquery

我正在做一个简单的 $.ajax 请求:

$.ajax({              
    type: "POST",

    url: "/Run/" + param1 + "/" + param2,

    dataType: 'html',

    error: function(error) {

    },

    success: function(html) {

    }
});

如果我的 param2 值类似于 http://localhost/pub/file?val1=Some Text&val2=Some Text 然后使用 escape(param2)、encodeURI(param2)、encodeURIComponent(param2) 完成编码没有帮助。我收到以下错误 -->

HTTP 错误 400.0 - 错误请求 ASP.NET 检测到 URL 中存在无效字符

我的问题-->

  1. 我应该如何编码param2
  2. $.ajax 调用中请求 URL 的最大长度是多少?
  3. 请求 URL 最大长度是否取决于发出请求的浏览器类型?
  4. 我观察到,如果我使用 Ajax.ActionLink,那么我不需要对传递给操作的参数进行编码,并且可以将长度 > 10,000 个字符的参数传递为出色地。但我不知道如何使用 java 脚本中的 Ajax.ActionLink 进行显式调用。我需要点击该操作链接才能通过 Ajax.ActionLink 进行调用。

Ajax.actionLink 的优点--> 请查看使用 Ajax.ActionLink 传递给操作的参数 categoryName 的长度(这是我的观察) alt text

alt text

最佳答案

这么大的参数应该发布而不是在 URL 中发送。

$.ajax({              
    type: 'POST',
    url: '/Run',
    data: { param1: param1, param2: param2 },
    dataType: 'html',
    error: function(error) {
    },
    success: function(html) {
    }
});

这将自动处理参数编码。如果您绝对坚持在 url 中发送它们,您可以声明一个全局 javascript 变量来保存要调用的 url:

<script type="text/javascript">
    var url = '<%= Url.Action("Run"), new { param1 = "value1", param2 = "value2" } %>';
    $(function() {
        $.ajax({              
            type: 'POST',
            url: url,
            dataType: 'html',
            error: function(error) {
            },
            success: function(html) {
            }
        });
    });
</script>

关于asp.net-mvc - 与 Ajax.ActionLink 相比,$.ajax 调用中的请求 URL 编码问题以及 $.ajax 中的最大请求 URL 长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2340045/

相关文章:

c# - 尝试上传时 Controller 操作未从 View 中抓取文件

javascript - 鼠标移开 15 秒后隐藏 div

javascript - 有没有办法从 ckeditor 中单击并保存内容然后关闭?

javascript - 设置一次日期格式,因此 jquery 和 jqueryUI 日期选择器使用该日期格式

c# - 为什么我得到 'One or more types required to compile a dynamic expression cannot be found.' ?

c# - MySql 数据阅读器不检索行

asp.net-mvc - "It is not possible to profile 32-bit and 64-bit applications at the same time"错误

css - 更改 Kendo UI Validator 的位置

javascript - 单击按钮后如何在 Dropzone.js 中调用 .removeAllFiles() 函数

jquery - JavaScript 返回不起作用?返回未定义