0

I'm trying to send a pretty big string (about 3 milion and a half characters) using ajax. The code works fine with small strings but if I try to use the string I need to pass it won't work. The string I use is an encoded base64 Excel file.

This is the code I use to send the string:

$.ajax({
      url: "", //The page I call
      type: "GET",
      data: { base64: "....."},//here goes the string I need to pass
      dataType: 'text',
      success: function (response) {
      alert("successful");
      },
      error: function () {
          alert("error");
      }
});

This code works with smaller strings but not with mine. Again the string I'm trying to pass is about 3 milion and a half characters and could even be bigger.

The backend code is the following:

[HttpGet("Test")]
        public async Task<string> testHelloWorld(string saluto)
        {

            try
            {
                string testString = "Funziona";
                Console.Write(testString + ": " + saluto + "\n");
                return testString;
            }
            catch (Exception ex)
            {
                Log.Error("API(Test) - Exception", ex);
                return null;
            }
        }

Is there a way to send this string??

Synapsis
  • 667
  • 1
  • 5
  • 19

3 Answers3

2

Use POST instead of GET because very often there is a limit for length of query string configured on web-server.

mtkachenko
  • 5,389
  • 9
  • 38
  • 68
1

For GET request, HTTP itself doesn't impose any hard-coded limit, but browsers have limits ranging on the 2kb - 8kb. The limit is dependent on both the server (Apache, IIS, NGINX, ...) and the client (browser) side.

Use POST request for big string. If it still causes problems, try to compress the data.

emert117
  • 1,268
  • 2
  • 20
  • 38
0

Change your ajax to POST and change the jsonSerialization max lenght on the web.config file.

The default value is 2097152 characters. Change it to 2147483644 (int max lenght) and check again.

<configuration> 
   <system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="2147483644"/>
           </webServices>
       </scripting>
   </system.web.extensions>
</configuration> 

Using GET, the max url lenght is 2083 characters.

Renan Araújo
  • 3,533
  • 11
  • 39
  • 49