Receive JSON Array response from Java servlet in UTF-8 charset -


i doing ajax call java servlet. servlet responds json array charset set utf-8. however, once response in ajax call, ??? characters in strings. went through lot of testing , research , not find possible solution.

ajax call:

                  $.ajax({                     type: 'post',                     data: {curtablename: curtablename,curtableid: curtableid},                     datatype: 'json',                     url: '../showproducts',                     success: function(productinfo){                         var noofproducts = productinfo.length;                         for(var = 0; < noofproducts; i++)                         {                             product.push(productinfo[i].product.substr(0,25) + "...");                             webshop.push(productinfo[i].webshop);                             price.push(productinfo[i].price);                             availability.push(productinfo[i].availability);                             lastchecked.push(productinfo[i].lastchecked);                             checkfreq.push(productinfo[i].checkfreq);                             url.push(productinfo[i].url);  displayproductinfo(product[i],webshop[i],price[i],availability[i],lastchecked[i],checkfreq[i],url[i]);                         }                     }                    }); 

and java servlet response:

            response.setcontenttype("application/json");             response.setcharacterencoding("utf-8");             response.getwriter().write(jsonarr.tostring()); 

based on own research java servlet seems correct, , maybe there issue javascript. has ideas. appreciated. :)

i have managed work. problem resided in fact json needed encoded in servlet, , decoded in javascript. thank - isah.

code changes:

ajax call:

     $.ajax({                     type: 'post',                     data: {curtablename: curtablename,curtableid: curtableid},                     datatype: 'json',                     url: '../showproducts',                     success: function(productinfo){                         var noofproducts = productinfo.length;                         for(var = 0; < noofproducts; i++)                         {                             productinfo[i].product = decodeuricomponent(productinfo[i].product);                             productinfo[i].product = productinfo[i].product.replace(/\+/g, ' ');                             product.push(productinfo[i].product.substr(0,25) + "...");                             webshop.push(productinfo[i].webshop);                             price.push(productinfo[i].price);                             availability.push(productinfo[i].availability);                             lastchecked.push(productinfo[i].lastchecked);                             checkfreq.push(productinfo[i].checkfreq);                             url.push(productinfo[i].url);  displayproductinfo(product[i],webshop[i],price[i],availability[i],lastchecked[i],checkfreq[i],url[i]);                         }                     }                    });  

java servlet:

productsplit[0] = urlencoder.encode( productsplit[0], "utf-8"); 

additional line has been added java servlet. title gets encoded, , after goes json object.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -