G.D G.D - 3 months ago 9
Ajax Question

Ajax parameters are encrypted when passed to Java contoller

I have below object named itemInfo in Javascript-

itemInfo = {

catalogId:[{"catId":"RS-122-2001","quantity":"1"},{"catId:"RS-122-2002","quantity":"1"}]

}


I am passing this object through ajax to a java controller-

$.ajax({
type: 'GET',
url: '/bin/path/servlet',
dataType: 'text',
data: {
productId: itemInfo.catalogId,
operation: "addToCart"
},
success: function(obj) {
if (obj) {
//do something
}
},
error: function(request, status, error) {
console.log(request.responseText);
}

});


When I see the request, its sent like this-

<<hostName>>/bin/path/servlet?productId%5B0%5D%5BcatId%5D=RS-122-2001&productId%5B0%5D%5Bquantity%5D=1&productId%5B1%5D%5BcatId%5D=RS-122-2002&productId%5B1%5D%5Bquantity%5D=1&operation=addToCart


Decoded URL parameters looks like this-

productId[0][catId]=RS-122-2001&productId[0][quantity]=1&productId[1][catId]=RS-122-2002&productId[1][quantity]=1&operation=addToCart


Its not making any sense in Java and passed values are not readable. Is something wrong with implementation? How can I retrieve passed values in Java controller?

Answer

Try to stringify your data:

productId: JSON.stringify(itemInfo.catalogId)

By the way i don't think is a good idea to post complex object by "GET" method. I suggest to change your servlet to accept method "POST" then modify your script type to "POST".

Comments