1

jsonからオブジェクトを取得successしたいajaxこれは私がこれまでに行ったことです

私のドロップダウンリスト

<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自分のサーブレットでコードを使用しています:

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

成功のコールバック関数が JSON 結果の解析に失敗しているようです。

次のような URL クエリ文字列としてサーブレットに値を渡してみてくださいhttp://localhost/project/Analysis?team=some-value-you-know

次に、http://jsonlint.comなどのツールを使用して出力された JSON を検証し、生成している JSON を修正します [おそらく JSON オブジェクト キーを二重引用符で囲みます]。

また、ブラウザで開発者ツールの JavaScript コンソールを使用してみてください。JavaScript エラーを解決するのに役立ちます。

これが役に立てば幸いです、
Shareb。

于 2013-09-23T01:49:40.140 に答える
0

ばかげた私は、サーブレットからコードを変更する答えを見つけました:

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 に答える