2013-04-04 80 views
1

我有一個JSON頁面的內容將取決於成功值{"success":1} or {"success": 0},我必須顯示錯誤消息或成功消息。如果頁面不可用,那麼我也應該給出錯誤信息。我在加載div中有一個微調:這是我的代碼。在getJSON中不調用失敗函數

url = "add.jsp"; 
$("#loading").show(); 
$.getJSON(url, dataToBeSent, function(data) { 
    $.each(data, function(key, val) { 
     if (val == 0) { 
      $("#loading").hide(); 
      $("#add_response").show(); 
      $("#add_response #error_message").show(); 
      $("#add_response #success_message").hide(); 
     } else { 
      $("#loading").hide(); 
      $("#add_response").show(); 
      $("#add_response #error_message").hide(); 
      $("#add_response #success_message").show(); 
     } 
    }) 
    .fail(function(){ 
     $("#loading").hide(); 
     $("#add_response").show(); 
     $("#add_response #error_message").hide(); 
     $("#add_response #success_message").show(); 
    }); 
}); 

在我的控制檯中沒有錯誤消息。我沒有文件add.jsp。因此getJSON應該失敗。我究竟做錯了什麼?請幫我找出答案。爲什麼失敗根本不會被調用?

追問:這是繼答案由Hardik建議代碼

<script type="text/javascript"> 
var url = "add.jsp"; 
$("document").ready(function() { 
    $('input[name=submit]').click(function(e) { 
     var dataToBeSent = $("form").serialize(); 
     var url = 'db.jsp'; 
     $.ajax({ 
      url : url, 
      data : dataToBeSent, 
      dataType : 'json', 
      success : function(response) { 
       $('#response').text(response.success); 
      }, 
      error : function(request, textStatus, errorThrown) { 
       alert(request.status + ', Error: ' + request.statusText); 
       // perform tasks for error 
      } 
     }); 
     e.preventDefault(); 
    }); 
}); 
</script> 

回答

1
<script type="text/javascript" src="js/jquery.min.js"></script> 
// I have jquery.js under js directory in my webapp 
<script type="text/javascript"> 
    var url = "add.jsp"; 
    $(function(){ 
     $.ajax({ 
      url : url, // Pass you Servlet/ JSP Url 
      data : { 
       empId : 0 
      }, // data can be passed from outside 
      dataType : 'json', 
      success : function(response) { 
       alert('Success'); 
          // perform tasks for success 
      }, 
      error : function(request, textStatus, errorThrown) { 
       alert(request.status + ', Error: ' + request.statusText); 
          // perform tasks for error 
      } 
     }); 
}); 
</script> 

EDIT--例子:

<script type="text/javascript"> 
    $(function(){ 
     function getData(url, dataToBeSent) { 
      console.log(dataToBeSent); 
      $.ajax({ 
       url : url, 
       data :dataToBeSent, 
       dataType : 'json', 
       success : function(response) { 
        alert(response.success); 
       }, 
       error : function(request, textStatus, errorThrown) { 
        alert(request.status + ', Error: ' + request.statusText); 
       } 
      }); 
     } 
     getData('getData.jsp', '{name:Hardik}'); // You should use Servlet to get Data. This is just an example 
}); 

getData.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<% 
response.setContentType("application/json"); 
out.println("{\"success\":1}"); // Write values using JSONObject 
%> 
+0

如何從JSON文件獲取數據?我是否需要在成功函數中寫入getJSON方法?這需要兩倍的時間。我試圖打印變量響應,但它是空白的。 – NewUser 2013-04-04 06:45:00

+0

不需要。您不需要再次調用'getJSON'。上面的例子本身就是一個例子。傳遞你的Servlet/JSP。我添加了錯誤的JSP名稱以方便錯誤示例。 – 2013-04-04 06:49:16

+0

您以哪種格式發送'dataToBeSent'? – 2013-04-04 06:53:19