ajax查詢資料庫,伺服器傳回json字串,js解析json

來源:互聯網
上載者:User

標籤:ajax   伺服器   json   js   

伺服器端獲得要查詢的東西,查詢資料庫,將查詢的資訊,以json字串的形式返回給瀏覽器

public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        String departmentID=request.getParameter("departmentID");        DataBaseHandle dataBaseHandle=new DataBaseHandle("commonUser", "common", "jdbc:mysql://127.0.0.1:3306/firstwebsite");        String SQL="select * from speciality where departmentID=‘"+departmentID+"‘";        String json="{‘specialities‘:[";        response.setContentType("text/html; charset=utf-8");        response.setCharacterEncoding("UTF-8");        try {            ResultSet rs=dataBaseHandle.exeuteSQL(SQL);            while(rs.next())            {                json=json+"{‘"+"specialityID‘:‘"+rs.getString(1)+"‘,"+"‘SPname‘:‘"+rs.getString(2)+"‘},";                System.out.println(rs.getString(1)+rs.getString(2));            }            json=json.substring(0,json.length()-1);            json=json+"]}";            PrintWriter pwriter=response.getWriter();            pwriter.print(json);            rs.close();            dataBaseHandle.closeResourse();        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }

js解析json,獲得返回的內容

   function updateSpeciality(str)        {            var xmlhttp;            if (str=="")            {                alert("請選擇非空的值!");                return;            }            if (window.XMLHttpRequest)            {// code for IE7+, Firefox, Chrome, Opera, Safari                xmlhttp=new XMLHttpRequest();            }            else            {// code for IE6, IE5                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");            }            xmlhttp.onreadystatechange=function()            {                if (xmlhttp.readyState==4 && xmlhttp.status==200)                {                    var dataObj=eval("("+xmlhttp.responseText+")");//轉換為json對象                    for(var o in dataObj.specialities)                    {                        alert(dataObj.specialities[o].specialityID);                        alert(dataObj.specialities[o].SPname);                    }                }            }            xmlhttp.open("GET","/First/servlet/GetMessage?departmentID="+str,true);            xmlhttp.send();        }

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

ajax查詢資料庫,伺服器傳回json字串,js解析json

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.