0

json オブジェクトを使用して jsp ファイルからデータを転送しようとしています。

これは私のJavaScriptコードです:

// action when item file is clicked
$("li span.file").click(function(){

// get the ID
alert(' Forward the url when clicked WITH ID => ' + $(this).attr('id'));

$.getJSON('BomItemToJSON', function(data) {
    alert('entered getJSON()');
    $.each(data, function(i, item) {
        alert('entered each()');
        var id = item.id;
        var description = item.description;

        alert('description: ' + description);

        formObject = document.forms['itemForm'];
        formObject.elements['itemId'].value = id;
        formObject.elements['itemDescription'].value = description;

        alert('done with javascirpt');
    });
});

});

これは、JavaScript によって呼び出される必要がある私のサーブレットです。

public class BomItemToJSON extends HttpServlet {

private static final long serialVersionUID = 1L;

@PersistenceContext(unitName = "xxx")
public EntityManager em;

@Resource
UserTransaction utx;

    Logger logger = Logger.getLogger(this.getClass().getCanonicalName());


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    System.out.println("bom item to json servlet has been called.");
    try {
        utx.begin();
    } catch (NotSupportedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SystemException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //int id = Integer.parseInt(request.getParameter("id"));
    BomHandling bh = new BomHandling(em, utx);

    BomItem item = bh.getBomItem(63788);
    Gson gson = new Gson();
    String json = gson.toJson(item);

    System.out.println("Json: " + json);

    response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(json);

    try {
        utx.commit();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (RollbackException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (HeuristicMixedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (HeuristicRollbackException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SystemException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

これは、サーブレットをマップする私の web.xml です。

 <servlet>
  <display-name>BomItemToJSON</display-name>
  <servlet-name>BomItemToJSON</servlet-name>
  <servlet-class>com.xxx.servlets.BomItemToJSON</servlet-class>
 </servlet>

<servlet-mapping>
  <servlet-name>BomItemToJSON</servlet-name>
  <url-pattern>/BomItemToJSON</url-pattern>
 </servlet-mapping>

アイテムをクリックすると、「IDでクリックするとURLを転送します」というアラートが表示されます。しかし、 getJSON() 関数が呼び出されていないようです。なんで?

4

1 に答える 1

0

不足しているものを見つけるためのいくつかのデバッグのヒント:

  1. Firefox 用の Firebug を使用します (他の同等品はあまり良くないと思います)。
  2. アラートの代わりに console.log("text") を使用してください。それらはIEなどで壊れるので、忘れずに削除/コメントアウトしてください。
  3. Firebug の [スクリプト] タブでデバッガーを使用して、最初にコードが getJson 関数に到達することを確認します。また、コールバック関数をデバッグすることもできます。
  4. net タブを使用して、/BomItemToJSON url が呼び出されたかどうか、およびブラウザーにどのような応答が返されたかを確認します。

また、私は個人的に $.ajax 関数を jQuery でのみ使用し、省略形では使用しないことを好みます。

于 2012-04-17T09:00:59.833 に答える