1

I have the below js code:

xmlhttp = GetXmlHttpObject();
if (xmlhttp != null) {
    var url = "/support/residential/outage/serviceOutage?cbrnum=" + cbrnum + "&btn=" + btn + "&outagetkt=" + outagetkt;
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("thankyoucontent").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST", url, true);
    xmlhttp.send(null);
}

This calls a servlet by passing certain params in query string. Now my question is , can we bind all the params into a single object , send that object alone in query string and use the object in the servlet to retrieve the param values? Is it possible?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143

1 Answers1

2

Yes, you can send the data as JSON to the server:

var data = {cbrnum: cbrnum, btn: btn};
var url = "...?data=" + encodeURIComponent(JSON.stringify(data));

Then on the server side, retrieve the value of data and parse the JSON into native data types (Converting JSON to Java).

Note though that browsers often impose a length limit on the URL. If you have a lot of data, do a POST request instead.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143