jquery和servlet如何傳遞JSON數(shù)組
例 3.7(AjaxJsonArray.jsp)
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
<html>
<head>
<script src="jquery.js"></script>
<script type="text/javascript">
function getP(id) {
$.getJSON("jqueryservlet37", {
id : id
}, function(arr) {
for ( var i = 0; i < arr.length; i++) {
var obj = arr[i];
for ( var pro in obj) {
document.getElementById(pro).innerHTML = obj[pro];
}
}
/* document.getElementById(pro).innerHTML = obj[pro];和下一句一樣。
$("#"+pro).html(obj[pro]);
*/
})
}
</script>
</head>
<body>
<table width="340" border="1">
<tr>
<td id="1" onclick="getP('1')">點(diǎn)這1</td>
<td id="2" onclick="getP('2')">點(diǎn)這2</td>
</tr>
</table>
<table width="340" border="1">
<tr>
<td width="170">參數(shù)項(xiàng)</td>
<td width="170">參數(shù)值</td>
</tr>
<tr>
<td>型號(hào)</td>
<td id="type"> </td>
</tr>
<tr>
<td>價(jià)格</td>
<td id="price"> </td>
</tr>
<tr>
<td>型號(hào)1</td>
<td id="type1"> </td>
</tr>
<tr>
<td>價(jià)格1</td>
<td id="price1"> </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;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class jqueryservlet37 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");
JSONArray arr = new JSONArray();
JSONObject o = new JSONObject();
o.put("type","adslqixy"+id);
o.put("price","333qixy"+id);
arr.add(o);
o = new JSONObject ();
o.put("type1","adslqixy"+id+id);
o.put("price1","666qixy"+id+id);
arr.add(o);
System.out.println("arr.toString() is" + arr.toString());
out.print(arr.toString());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}