Note that there are some explanatory texts on larger screens.

plurals
  1. POgetJSON() JavaScript function not called
    text
    copied!<p>I am trying to transfer data from a jsp file using a json object.</p> <p>This is my JavaScript code:</p> <pre><code>// action when item file is clicked $("li span.file").click(function(){ // get the ID alert(' Forward the url when clicked WITH ID =&gt; ' + $(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'); }); }); }); </code></pre> <p>This is my Servlet which should be called by JavaScript:</p> <pre><code>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(); } } </code></pre> <p>}</p> <p>This is my web.xml which maps the Servlet:</p> <pre><code> &lt;servlet&gt; &lt;display-name&gt;BomItemToJSON&lt;/display-name&gt; &lt;servlet-name&gt;BomItemToJSON&lt;/servlet-name&gt; &lt;servlet-class&gt;com.xxx.servlets.BomItemToJSON&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;BomItemToJSON&lt;/servlet-name&gt; &lt;url-pattern&gt;/BomItemToJSON&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p>When I click on the item, I get the alert saying "Forward the url when clicked with id". But it seems like that the getJSON() function is not called. Why?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload