2012-04-17 92 views
0

我試圖使用json對象從jsp文件傳輸數據。getJSON()JavaScript函數未調用

這是我的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(); 
    } 
} 

}

這是我的web.xml它映射了Servlet :

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

當我點擊該項目時,我收到警告,提示「點擊時轉發URL」。但似乎沒有調用getJSON()函數。爲什麼?

+1

指定的URL「BomItemToJSON」實際上是否存在?如果是,它會返回什麼?你的JS控制檯有錯誤嗎? – m90 2012-04-17 08:49:06

+0

在任何現代瀏覽器中,打開調試工具並查看「網絡」選項卡或類似內容。您可能會看到一個404錯誤或類似的錯誤,這可能表明URL映射有問題,或者500錯誤,這表明servlet有問題。如果你看不到,你應該看到響應數據,這可能會幫助你弄清楚發生了什麼。 – 2012-04-17 08:50:41

+1

與您的實際問題無關,但您爲什麼要返回帶有內容類型「text/plain」的JSON? JSON的正確內容類型是['application/json'](http://tools.ietf.org/html/rfc4627)。 (我說它是無關的,因爲'getJSON'將忽略內容類型並嘗試將結果解釋爲JSON。但是,當你明確地設置內容類型時,可以正確設置它。) – 2012-04-17 08:51:20

回答

0

一些調試技巧找到什麼丟失:

  1. 使用Firefox的Firebug(我還是找到其它等價物的那麼好)
  2. 使用的console.log(「文本」),而不是警報,他們沒有破壞性。請記住刪除它們/將它們註釋掉,因爲它們在IE中斷等。
  3. 在Firebug的腳本選項卡中使用調試器來檢查您的代碼是否首先到達getJson函數。你也可以調試回調函數。
  4. 使用net選項卡查看您的/ BomItemToJSON url是否被調用,以及給予瀏覽器什麼響應。

此外,我個人喜歡只在jQuery中使用$ .ajax函數,而不是它們的簡寫。