1

我想从中获取对象json这是我到目前为止所做的successajax

我的下拉列表

<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>

这是我Ajax的,select上面是通过循环迭代的,所以我必须使用每个来知道我正在使用哪个下拉列表(只是为了通知你们)

$('.tester_team').each(function(){
    $(this).change(function() {

          $.ajax({
         url: 'Analysis',
         type: 'POST',
         dataType: 'json',
         data: {team: $(this).val()},
         success: function(data){
             alert(data); // alert not working

         }
          });

    }); 
});

Gson在我的 servlet 上使用以下代码:

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 );
    }
}

问题出在脚本中,当我更改下拉列表时Ajax不会触发函数我的代码有什么问题?alert(data);还是我滥用了代码?

4

2 回答 2

0

愚蠢的我,好吧,我发现答案从 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 );
    }
}

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;
}

我刚刚添加return;

于 2013-09-23T05:39:23.873 回答
0

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

尝试将值作为 URL 查询字符串传递给您的 servlet,例如http://localhost/project/Analysis?team=some-value-you-know

然后使用http://jsonlint.com之类的工具验证打印的 JSON,修复您正在生成的 JSON [可能是 JSON 对象键周围的双引号]。

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

希望这会有所帮助,
Shareb。

于 2013-09-23T01:49:40.140 回答