0

I'm attempting to submit several parameters using Jquery post method, here is how I wrote the code. Jquery:

$(document).on("input",".li-add input", function() {
     var input = $("#add-purposes").val();
     if(input != "") {
      $("#outter-d-1").load("/UniHealths/UNCManageDataDictionaryServlet?type=search #d-1", {
       queryString:input,
       resultUrl:"/dictionary/treatment",
       resultSetName:"dictionary_conditions",
       queryType:["CONDITION","SYMPTOM"]
      });
     } else {
      $("#d-1").empty();
     }
    })
I use queryType as the parameter name, and it has two values in the array, when I use HttpServletRequest's method getParameter("queryType") from server, I got a null pointer, why this happens? how can i submit two different values with a same name?
JoeyW
  • 1
  • 1

1 Answers1

1

As of jQuery 1.4, the $.param() method serializes deep objects recursively to accommodate modern scripting languages and frameworks such as PHP and Ruby on Rails. You can disable this functionality globally by setting jQuery.ajaxSettings.traditional = true;

This also means, that arrays will be serialized as paramName[] (note the [] at the end).

So in your case the server receive param with name: queryType[], not queryType.

To alter this jQuery behaviour you can set the jQuery.ajaxSettings.traditional = true; before sending the request.

But beware:

As of jQuery 3.0, the $.param() method no longer uses jQuery.ajaxSettings.traditional as its default setting and will default to false.

So if you use jQuery 3.x, you shoud explicitly set the traditional option in the settings of your request.

As far as I know, the .load() function doesn't support settings parameter, so use .ajax() instead.

The settings parameter should include traditional: true if you want to serialize arrays without adding [] to param name.

Ruslan Stelmachenko
  • 4,987
  • 2
  • 36
  • 51