jquer和servlet如何傳遞json
例 3.6
馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203。
<html>
<head>
<script src="jquery.js"></script>
<script type="text/javascript">
function getP(id) {
$.getJSON("jqueryservlet36", {
id : id
}, function(obj) {
for ( var p in obj) {
//Servlet 返回值是{type:'"+type+"',price:'"+price+"'}"這樣的JSON對象
//$("#info").toggle();找id為info的元素
$("#" + p).html(obj[p]);
/* document.getElementById(p).innerHTML = obj[p];和下一句一樣。
$("#"+p).html(obj[p]);
*/
}
})
}
</script>
</head>
<body>
<table width="340" border="1">
<tr>
<td id="1" onclick="getP('1')">點這1</td>
<td id="2" onclick="getP('2')">點這2</td>
</tr>
</table>
<table width="340" border="1">
<tr>
<td width="170">參數(shù)項</td>
<td width="170">參數(shù)值</td>
</tr>
<tr>
<td>型號</td>
<td id="type"> </td>
</tr>
<tr>
<td>價格</td>
<td id="price"> </td>
</tr>
</table>
</body>
</html>
package helloWorld;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class jqueryservlet36 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String id = request.getParameter("id");
String str = "";
String type = "";
String price = "";
type = "qixyADSL" + id;
price = "qixy333" + id;
str = "{type:'" + type + "',price:'" + price + "'}";
out.print(str);
System.out.print(str);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}