为什么jquery无法与后台的Servlet建立连接呢?感觉代码没错啊。
代码如下:
index.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

<script type="text/javascript" src="scripts/jquery-1.8.3.js"></script>

<script type="text/javascript">

$(function()
{
$("#button1").click(function()
{
$.get("GetImageServlet",
{
categoryName: $("#categoryName").val();
picSource: $("input[name=picSource]:checked").val();

}, function(returnedData)//回调
{
alert(returnedData);  //test

$("#div1").empty(); //每次点击时先清空

var html = "";

for(var i = 0; i < returneData.length; i++)
{
html += "<img src='" + returnedData[i] + "' width='150' height='150'>";

}

$("#div1").append(html);

});
});


$("#button2").click(function()
{
$("#div2").html("<font color='red'>processing</font>");

$.post("SaveImageServlet",{}, function()
{


});

});


});

</script>





  </head>
  
  <body>
    
    <input type="text" id="categoryName"/>
    <input type="radio" name="picSource" value="google" checked>Google
    <input type="radio" name="picSource" value="flickr" />Flickr
    <input type="button" value="click" id="button1">"
    <input type="button" value="save" id="button2"/>
    
    <div id="div2"></div>
    
    <div id="div1"></div>
    
  </body>
</html>
后台GetImageServlet代码:
package com.imagetools;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.json.JSONArray;
import org.json.JSONObject;import com.google.gson.Gson;public class GetImageServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{

//连接测试
System.out.println("WWWWWWW" + req.getParameter("categoryName") + ", " + req.getParameter("picSource"));

resp.getWriter().print("hello world");

resp.getWriter().flush();


}



@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
this.doGet(req, resp);
}


}代码来自张龙老师的教学视频。jqueryservlet建立连接