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

});

这是我的 Servlet,应该由 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();
    }
}

}

这是我映射 Servlet 的 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 是否被调用以及向浏览器提供什么响应。

另外,我个人喜欢只在 jQuery 中使用 $.ajax 函数,而不是它们的简写形式。

于 2012-04-17T09:00:59.833 回答