1

我有一个 JSON 页面,内容{"success":1} or {"success": 0}取决于成功值,我必须显示错误消息或成功消息。如果页面不可用,那么我也应该给出错误消息。我在加载 div 中有一个微调器:这是我的代码。

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

1 回答 1

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>

编辑——例如:

<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   
});

获取数据.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
%>
于 2013-04-04T05:54:36.767 回答