Request a URL encoding problem in a $ .ajax call and the maximum request URL length in $ .ajax compared to Ajax.ActionLink

I am making a simple $ .ajax request:

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

If my param2 value is like http: // localhost / pub / file? Val1 = Some Text & val2 = Some Text strong>, then the encoding is done using escape (param2), encodeURI (param2), encodeURIComponent (param2) does not help. And I get the following ERROR →

HTTP Error 400.0 - Invalid ASP.NET Request Detected Invalid Characters in URL

My questions →

  • How do i encode param2 ?
  • What is the maximum length of a request url in a $ .ajax call?
  • Is it asking for the maximum URL length depending on the type of browser from which the request is made?
  • I noticed that if I use Ajax.ActionLink , then I do not need to code the parameters passed to the action, and I can pass parameters> 10,000 characters in length. But I do not know how to make an explicit call using Ajax.ActionLink from my java script. I need to click this action link to make a call through Ajax.ActionLink .

Advantages of Ajax.actionLink -> Please view the length of the categoryName parameter passed to the action using Ajax.ActionLink (This is my observation) alt text

alt text

+4
source share
1 answer

Such large parameters should be sent and not sent in the URL.

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

This will automatically handle the encoding of the parameters. If you absolutely insist on sending them to the url, you can declare a global javascript variable that will contain the url to call:

 <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> 
+3
source

Source: https://habr.com/ru/post/1302455/


All Articles