2012-08-07 108 views
0

我想通過JQuery發出AJAX請求 以下是我的代碼。Jquery無法向服務器發出Ajax請求

但是當我通過Mozilla Firebug進行調試時,我觀察到,沒有請求打到服務器。

請問有人能告訴我我做錯了什麼。

<%@page contentType="text/html" pageEncoding="UTF-8"%> 



<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JQuery Example</title> 
    </head> 
    <body> 
     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
     <script type="text/javascript"> 
      $(document).ready(function(){ 
         $.ajax({ 
         url: 'ajax/balances', 
         processData: false, 
         timeout: 10000, 
         type: "POST", 
         contentType: "application/xml", 
         dataType: "json", 
         data: '<MyReq user="' + User + '" katha="' + ivalitiKatha + '" />', 
        success: function(data) { 

        }, 
        error : function() { 
          alert("Sorry, The requested property could not be found."); 
        } 
      }); 

       }); 

     </script> 
      </body> 
</html> 

這是我在服務器端的web.xml

<servlet-mapping> 
     <servlet-name>Jersey Web Application</servlet-name> 
     <url-pattern>/ajax/*</url-pattern> 
    </servlet-mapping> 
+0

$阿賈克斯代碼對我的作品!當我在Jquery所在的頁面上使用您的代碼時,發送請求!您可以通過將代碼複製到螢火蟲控制檯並執行它來嘗試。你的$ .ajax代碼不是問題 – 2012-08-07 07:56:20

+0

'User'和'ivalitiKatha'在哪裏?也許是因爲那些沒有定義的變量? – 2012-08-07 08:31:37

回答

1

也許將<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>的頭部,而不是身體有幫助!

0

首先我建議將CDN JQuery移動到網站的頭部分。

其次,我已經測試了上面的代碼,並且問題看起來與您在JSON/AJAX請求中發佈的(數據)一致。

如果您將其刪除或修改爲JSON,請求會返回結果

$.ajax({ 
    url: 'test', 
    processData: false, 
    timeout: 10000, 
    type: "POST", 
    contentType: "application/json", 
    dataType: "json", 
    data: '{"foo": "bar"}', 
    success: function(data) { 
     alert('Success');     
    }, 
    error : function() { 
     alert("Sorry, The requested property could not be found."); 
    } 
});​ 

您需要將數據格式化爲JSON請求

data: '{"foo": "bar"}', 

希望這有助於