1.客户端代码
<script type="text/javascript">
function oncl(){
$.getJSON("http://192.168.9.55:17003/Test1/jsonPServlet?callback=?",
{name:"张三"},function(json){
alert(json.flag);
alert(json.name);
});
}
</script>2.服务端代码
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String callback = request.getParameter("callback");
String name = request.getParameter("name");
Map<String, Object> map = new HashMap<String, Object>();
PrintWriter out = response.getWriter();
map.put("flag", true);
map.put("name", name);
out.write(callback+"("+JSONObject.fromObject(map).toString()+")");
}
本文介绍了一个简单的JavaScript客户端代码实例,用于通过GET请求调用服务端接口,并解析JSON响应来获取特定信息。客户端通过设置参数、发送请求、接收并处理JSON数据来展示了一个基本的Web应用交互过程。
1863

被折叠的 条评论
为什么被折叠?



