Tania Marinova
Tania Marinova

Reputation: 1898

my ajax call doesn' send utf - 8 parameter to my server

I've got a web service in java with method FindEl(string myel) that accepts utf - 8 string paraeter

the select query should find all the elements that start with this string Here is the code in Java - for my web service
public class locselall {

public String FindEl(String myel ) throws ClassNotFoundException 
{
//
    String selectQuery = "select biz_subject from pl_biz WHERE biz_subject ILIKE '"+ myel + "%'";
//get rows 
    }

And there is no problem when i type in the browser to test my web service and it i selects:

http://localhost:9091/locselall/services/locselall/FindEl?myel=СИТ

it works;


and here is the html page that sends request to the server

html>
<head>
<script>

var xmlhttp;
    if (window.XMLHttpRequest) 
    {
        xmlhttp = new XMLHttpRequest();
    } 
    else 
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
function triming()
{

var strInput= document.getElementById('txtInput').value;
// for example I enter "ШИФ " - utf  8 cahracters
var newstr = strInput.replace(/[^\u0400-\u04FF0-9]/gi, '');

         xmlhttp.onreadystatechange = function() 
        { 
            if (xmlhttp.readyState==4 && xmlhttp.status==200) 
            {
                alert(xmlhttp.responseText);
                //var xmlObj = xmlhttp.responseXML;   
                //var textXML = xmlObj.documentElement.firstChild.firstChild.nodeValue;

            }

        }

        var url = "http://localhost:9091/locselall/services/locselall/FindEl?myel="+ newstr;

        document.getElementById('pr').innerHTML = url;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(); 


}

</script>

</head>
<body>
<input type= "text" id="txtInput"   />
<input type="button" id="btnSearch" onClick = "triming();"/>
<div id="pr"></div>


</body>
</html>

As you see i have an alert for the url and it's exactly the same as the url which i typed in the browser to test my web service - but the response from the server is with no records selected 3


I think the problem is that my variable newstr holds utf - 8 (cyrilic )chaarcters and it's not properly send to the server and as a result it cannot select any records!

What I've tried

  1. Added meta tag with charset = utf -8

nothing

  1. I read that he problem might be in my tomcat server and i added

URIEncoding = "utf-8" in the server.xml file

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" 
               URIEncoding="UTF-8"/>

Still nothing

Thanks in advance

Upvotes: 0

Views: 951

Answers (1)

Ian Atkin
Ian Atkin

Reputation: 6346

Try using encodeURIComponent on the URL that is sent. I seem to remember seeing this issue before, where it worked with latin alphabet characters, but Cyrillic failed.

var url = "http://localhost:9091/locselall/services/locselall/FindEl?myel=" + encodeURIComponent(newstr);

Upvotes: 1

Related Questions