Utf - xmlhttp请求对象的.open()方法的url参数中的8个字符

时间:2013-02-02 14:00:15

标签: javascript ajax utf-8

我有一个网络服务,试图在大数据库中找到以客户端html页面中输入的这三个字母开头的所有元素:

以下是Java中的代码 - 用于我的Web服务

public class locselall 
{



public String FindEl(String myel ) throws ClassNotFoundException 
{

    String strXml = "<response>";

    Class.forName("org.postgresql.Driver");
    try 
    {
        conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/plovdivbizloca", "postgres", "tan");
    }

    catch (SQLException ex) 
    {

        ex.printStackTrace();
    }


    Statement mystmt = null;
    String selectQuery = "select biz_subject from pl_biz WHERE biz_subject ILIKE '"+ myel + "%'";

    try {
        mystmt = conn.createStatement();
        ResultSet mysr = mystmt.executeQuery(selectQuery);
        ResultSetMetaData rsmd = mysr.getMetaData();
        int colCount = rsmd.getColumnCount();
        int numberrow = 0;
        strXml += "<rows>";

        while (mysr.next()) 
        {
            strXml += "<row id= '" + numberrow + "'>";
            for (int i = 1; i <= colCount; i++) 
            {
                String elementname = rsmd.getColumnName(i);
                String elementValue = mysr.getString(i);
                strXml += "<colunm colname='" + elementname
                        + "' colvalue='" + elementValue + "'/>";
            }
            numberrow++;
            strXml += "</row>";
        }
        strXml += "</rows>";

    } 
    catch (Exception ex) 
    {

    }
    strXml += "</response>";

    return strXml;
}

}

当我输入浏览器时没有问题

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

一切正常;


这是客户端html页面

<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;
//var newstr = strInput.replace(/[\p{L}]/gi, '');
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;
        alert (url);

        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>

如您所见,我有一个网址提醒,它与我在浏览器中输入以测试我的网络服务的网址完全相同 - 但这次我没有得到任何记录; 服务器仅返回,这意味着当它到达要选择的语句时无法执行

我认为问题是我的变量newstr应该包含utf-8元素并且它没有正确发送到服务器。因此它无法选择任何记录!

提前致谢

1 个答案:

答案 0 :(得分:0)

嗯,问题是,当您像Chrome这样的浏览器中使用http://localhost:9091/locselall/services/locselall/FindEl?myel=СИТ这样的网址时,它实际上会向此网址发出请求:

http://localhost:9091/locselall/services/locselall/FindEl?myel=%D0%A1%D0%98%D0%A2

要确保在代码中也发生这种情况,您必须使用encodeURIComponent

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