2012-09-25 53 views
1

我試圖從jQuery Mobile應用程序進行Ajax調用。該呼叫由目標服務器正確提供。但是,瀏覽器拒絕迴應。從jQueryMobile應用程序啓動Ajax調用

火狐說:

x: [object Object], m: error, e: [Exception... "Component returned failure 
code: 0x80004005 (NS_ERROR_FAILURE)" nsresult: "0x80004005 (NS_ERROR_FAILURE)" 
location: "JS frame :: file:///path/to/my/page/resources/js/jquery/jquery-1.7.1.min.js 
:: <TOP_LEVEL> :: line 4" data: no] 

和Chrome說:

x: [object Object], m: error, e: Error: NETWORK_ERR: XMLHttpRequest Exception 101 
Origin null is not allowed by Access-Control-Allow-Origin. 

我的理解是這樣的錯誤鏈接到一個跨域調用問題。我已經完成了我在jQuery和jQueryMobile文檔中找到的所有內容,但都沒有運氣。

我的頁面是

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 

    <title>Main Page</title> 

    <link rel="stylesheet" href="./resources/css/jquery/jquery.mobile-1.1.1.min.css" /> 

    <script src="./resources/js/jquery/jquery-1.7.1.min.js"></script> 
    <script type="text/javascript"> 
    jQuery(document).live("mobileinit", function(){ 
     jQuery.support.cors = true ; 
     jQuery.mobile.allowCrossDomainPages = true ; 
    }); 
    </script> 
    <script src="./resources/js/jquery/jquery.mobile-1.1.1.min.js"></script> 

    <script type="text/javascript"> 
    function send() { 
     // the request 
     jQuery.ajax({ 
      async:   false, 
      contentType: "application/x-www-form-urlencoded; charset=UTF-8", 
      crossDomain: true, 
      data:   {data: "abcd"} , 
      dataType:  "text", 
      error:   function(x, m, e) { 
       alert("x: " + x + ", m: " + m + ", e: " + e); 
      }, 
      processData: true, 
      success:  function(data){ 
       alert("Received: " + data); 
      }, 
      type:   "GET", 
      url:   "http://my/servlet/url" 
     }); 
    } 
    </script> 
</head> 

<body> 
    <div id="page1" data-role="page"> 
     <div data-role="header" data-position="fixed"> 
      <h1>Login</h1> 
     </div> 

     <div data-role="content"> 
      <a id="btn1" data-role="button" onclick="send();" >Go!!!</a> 
     </div> 
    </div> 
</body> 
</html> 
+0

您可以嘗試從http:// localhost打開演示。 – xiaohao

+0

@ xiaohao,我沒有明白你的意思。你能否提供更多細節?同時,請注意,在服務器上可訪問servlet時,文件系統正在加載頁面「Main Page」。 – Younes

+0

你可以閱讀http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin,也許解決方案是 – xiaohao

回答

0

我已經要求該服務器追加頭「訪問控制允許來源:*」解決問題的響應。對於使用Java EE的人,解決的辦法是添加這行代碼:

response.addHeader("Access-Control-Allow-Origin", "*"); 

,其中反應是HttpServletResponse的實例。

相關問題