Javascript 使用Gson的Ajax

Javascript 使用Gson的Ajax,javascript,jquery,ajax,json,jsp,Javascript,Jquery,Ajax,Json,Jsp,我想从ajax的成功中获得json对象,这就是我到目前为止所做的 我的下拉列表 <select id="testers_team" class="tester_team"> <optgroup label="Current Value"> <option><c:out value="${info.team}"></c:out></option> </optgroup> &l

我想从ajax的成功中获得json对象,这就是我到目前为止所做的

我的下拉列表

<select id="testers_team" class="tester_team">
    <optgroup label="Current Value">
        <option><c:out value="${info.team}"></c:out></option>
    </optgroup>
    <optgroup label="Teams">
        <c:forEach var="team" items="${requestScope.testers}">
            <option value="${team.key}">${team.key}</option>
        </c:forEach>
    </optgroup>
</select>
我在servlet上使用Gson,代码如下:


问题在于使用Ajax的脚本不会触发alertdata;函数当我更改下拉列表时,我的代码有什么问题?还是我误用了代码?

成功回调函数似乎无法解析JSON结果

尝试将值作为URL查询字符串传递给servlet,如http://localhost/project/Analysis?team=some-你知道的价值

然后使用诸如修复生成的JSON之类的工具验证打印的JSON[可能在JSON对象键周围使用双引号]

还可以尝试在浏览器中使用开发工具的JavaScript控制台,它将帮助您解决JavaScript错误

希望这有帮助,
Shareb.

我真傻,好吧,我找到了答案,从servlet更改代码:

if( testers != null )
{
    response.setContentType( "application/json" );
    response.setCharacterEncoding( "UTF-8" );
    try
    {
        response.getWriter().write( new Gson().toJson( testers ) );
        //this one is printing so that means it actually succeed to parse?
        System.out.println( team );
    }
    catch( IOException e )
    {
       // just want to test out if it really failed
       System.out.println( "failed" ); 
       log.debug( "Unable to get parse request", e );
    }
}


我只是加了一个回报

如果要将值打印到控制台,则必须在Servlet中使用PrinterWriter打印json响应。您可以发布所有servlet代码来验证servlet调用吗?如果没有打印,可能它有错误每当我运行代码时都没有堆栈跟踪,并且我已经发布了与问题相关的所有servlet post代码。不要担心HashMap,它不是空的。println已经向我证明了这一点。关于PrintWriter,如果我理解正确,我不打算从servlet编写html代码。谢谢你的提示。但是关于你提到的验证工具,我将如何使用这个网站?我实际上是web-development的新手。只需将生成的JSON对象粘贴到工具的文本区域,单击“验证”并检查结果,如果无效,您将得到一个解析错误。此外,您还可以单击常见问题以获取有关错误的更多信息以及如何解决错误的建议。
String team = request.getParameter( "team" );
HashMap<String, ArrayList<String>> testerList
              = new UsersDAO().getTestersOfAllTeams();
ArrayList<String> testers = testerList.get( team );

if( testers != null )
{
    response.setContentType( "application/json" );
    response.setCharacterEncoding( "UTF-8" );
    try
    {
        response.getWriter().write( new Gson().toJson( testers ) );
        //this one is printing so that means it actually succeed to parse?
        System.out.println( team );
    }
    catch( IOException e )
    {
       // just want to test out if it really failed
       System.out.println( "failed" ); 
       log.debug( "Unable to get parse request", e );
    }
}
if( testers != null )
{
    response.setContentType( "application/json" );
    response.setCharacterEncoding( "UTF-8" );
    try
    {
        response.getWriter().write( new Gson().toJson( testers ) );
        //this one is printing so that means it actually succeed to parse?
        System.out.println( team );
    }
    catch( IOException e )
    {
       // just want to test out if it really failed
       System.out.println( "failed" ); 
       log.debug( "Unable to get parse request", e );
    }
}
if( testers != null )
{
    response.setContentType( "application/json" );
    response.setCharacterEncoding( "UTF-8" );
    try
    {
        response.getWriter().write( new Gson().toJson( testers ) );
        //this one is printing so that means it actually succeed to parse?
        System.out.println( team );
    }
    catch( IOException e )
    {
       // just want to test out if it really failed
       System.out.println( "failed" ); 
       log.debug( "Unable to get parse request", e );
    }
    return;
}