0

For example, I'm currently implementing client side javascript that will use a POST if the additional parameters exceed IE's safety limit of 2048ish charachers for GET HTTP requests, and instead attach the parameters to the body in JSON format. My code looks similar to the following:

var URL = RESOURCE + "?param1=" + param1 + "&param2=" + param2 + "&param3=" + param3();
        if(URL.length>=2048) {
            // Use POST method to avoid IE GET character limit
            URL = RESOURCE;
            var dataToSend = {"param3":param3, "param1":param1, "param2":param2};
            var jsonDataToSend = JSON.stringify(dataToSend);
            $.ajax({
                type: "POST",
                data: jsonDataToSend,
                dataType: 'json',
                url: URL,
                async: true,
                error: function() {
                    alert("POST error");
                },
                success: function(data) {
                    alert("POST success");
                }
            });
        }else{
            // Use GET
            $.ajax({
                type: "GET",
                dataType: 'json',
                url: URL,
                async: true,
                error: function() {
                    alert("GET error");
                },
                success: function(data) {
                    alert("GET success");
                }
            });
        }

Is there a way of me avoiding writing out this ajax twice? Something like

if(URL.length>=2048) {
// Use POST instead of get, attach data as JSON to body, don't attach the query parameters to the URL
}

N.b. I'm aware that using POST instead of GET to retrieve data goes against certain principles of REST, but due to IE's limitations, this has been the best work around I have been able to find. Alternate suggestions to handle this situation are also appreciated.

James
  • 223
  • 1
  • 6
  • 15
  • 3
    What data is causing your GET request to exceed 2048 characters? If you're expecting that size of input, it should definitely be a POST request. But to answer the question, just extract the `$.ajax` to another function – CodingIntrigue May 01 '15 at 09:04
  • Multilingual data that changes based on the locale of the users browser settings are being requested through this call. There's about 4.4k characters – James May 01 '15 at 09:11
  • *data* is still vague. Images? Text? Data URI? 4.4k characters is *a lot* of text data, definitely too much to inject into a URL – CodingIntrigue May 01 '15 at 09:13
  • Text, now being attached to the body of a POST (Surprisingly, IE didn't actually complain with the 4.4k character URL. Everything was still working as expected. But this refactor to POST happened because that number may expand in the distant future and we don't want it breaking then.) – James May 01 '15 at 09:19

1 Answers1

1

The $.ajax method of jQuery gets an object with properties. So it's quite easy, to frist generate that object and a "standard setting" and modify them based on certain logic and finally pass it to one loc with the ajax call.

Principle:

 var myAjaxSettings = {
            type: "POST",
            data: jsonDataToSend,
            dataType: 'json',
            url: URL,
            async: true,
            error: function() {
                alert("POST error");
            },
            success: function(data) {
                alert("POST success");
            }
        }

  if ( <condition a> )
      myAjaxSettings.type = "GET";

  if ( <condition b> )
      myAjaxSettings.success = function (data) { ...make something different ... };

  $.ajax(myAjaxSettings);
Axel Amthor
  • 10,980
  • 1
  • 25
  • 44